Get Data From Server in ANdroid

First Require To Post Request on Server


HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("passhearurldata");//Pass Hear Your Url
try {
    HttpResponse response = httpclient.execute(httpget);
    if(response != null) {
        String line = "";
        InputStream inputstream = response.getEntity().getContent();
        line = conStr(inputstream);
        Toast.makeText(this, line, Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(this, "Unable to complete your request", Toast.LENGTH_LONG).show();
    }
} catch (ClientProtocolException e) {
    Toast.makeText(this, "Caught ClientProtocolException", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
    Toast.makeText(this, "Caught IOException", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
    Toast.makeText(this, "Caught Exception", Toast.LENGTH_SHORT).show();
}

After That You Require Parse Server Response Into String


private String conStr(InputStream is) {
    String line = "";
    StringBuilder total = new StringBuilder();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    try {
        while ((line = rd.readLine()) != null) {
            total.append(line);
        }
    } catch (Exception e) {
        Toast.makeText(this, "Stream Exception", Toast.LENGTH_SHORT).show();
    }
    return total.toString();
}

No comments: