Skip to content Skip to sidebar Skip to footer

Http Connection In Android

How to create an Http Connection to retrive a web page content to my android? Please post example code for this.

Solution 1:

Its http client,get example it might help

    HttpClient client;
    HttpGet method;
    String url;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    url="enter your url here";
    method = newHttpGet(url);
    try {
            client.execute(method);


        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
           setContentView(R.layout.main);
}
publicvoidexecuteHttpGet()throws Exception {
    BufferedReaderin=null;
    try {
        client = newDefaultHttpClient();
        HttpGetrequest=newHttpGet();
        request.setURI(newURI(url));
        HttpResponseresponse= client.execute(method);
        in = newBufferedReader
        (newInputStreamReader(response.getEntity().getContent()));
        StringBuffersb=newStringBuffer("");
        Stringline="";
        StringNL= System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();
        Stringpage= sb.toString();
        System.out.println(page);
        } finally {
        if (in != null) {
            try {
                in.close();
                } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

it access XML data in preview i have this example try.

Post a Comment for "Http Connection In Android"