• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps HttpConnection in Android

apjustin

Lurker
Hi all,

I have to port one application from Blackberry to Android

I'm new to Droid and I couldn't find out a simple way to establish a HttpConnection

This is my Blackberry code

private HttpConnection myConn = null;
private InputStream myIs = null;
public boolean checkWebConnection(String username, String password)
{
myConn = null;
myIs = null;
String thisUrl = Constants.URL+"/ServiceOrders.asmx/CheckWebConnection?TechCode="+username+"&TechPIN="+password;
try{
myConn = (HttpConnection) Connector.open(thisUrl);
myConn.setRequestMethod(HttpConnection.GET);
myConn.setRequestProperty("Connection", "Keep-Alive");
int rc = ((HttpConnection)myConn).getResponseCode();
if(rc != HttpConnection.HTTP_OK){
return false; //throw new IOException("HTTP response code"+rc);
}
myIs = myConn.openDataInputStream();
}
catch(Exception e)
{
Dialog.inform("checkWebConnection()"+e.toString());
}
finally
{
try{
myConn.close();
myIs.close();
}catch(Exception e){}
}
return false;
}

Can anyone tell me the Droid way of implementing this in a simplest way like using HttpConnection?
 
String sUrl="http://www.location.de/xml.zip";
int test = -1;

HttpURLConnection conn=null;
BufferedReader rd;
try
{
url = new URL(sUrl);

try {
URLConnection urlconn =(URLConnection)url.openConnection();
if (!(urlconn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");

conn = (HttpURLConnection)urlconn;
conn.setRequestMethod("GET");
conn.connect();
test=conn.getResponseCode();

}
catch (IOException e1)
{
Toast.makeText(this,"e1"+e1.toString() ,Toast.LENGTH_LONG).show();

}

try {
// InputStream in=new InputStream(conn.getInputStream());
// parse(conn.getInputStream());
// InputStreamReader r=new InputStreamReader(conn.getInputStream());




rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

String line;
while ((line = rd.readLine()) != null)
{
response=line;
Toast.makeText(this, response,Toast.LENGTH_LONG).show();
}

}

catch (IOException e)
{

// TODO Auto-generated catch block
Toast.makeText(this,"e2"+e.toString() ,Toast.LENGTH_LONG).show();
e.printStackTrace();
}

}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
Toast.makeText(this,"e3"+e.toString() ,Toast.LENGTH_LONG).show();
e.printStackTrace();
}
 
Back
Top Bottom