Harsha M V
Harsha M V

Reputation: 54989

Android Web Request

I am trying to send a JSON string to the server and retrieve back a resulting JSON from the web server. I am able to handle the posting part but not sure how to retrieve back the result in the same request.

private boolean sendFacebookDataToServer(String url)
{
    // conect to server
    // This method for HttpConnection
    boolean isDataSend=false;
    try {
        HttpClient client = new DefaultHttpClient();

        HttpPost request = new HttpPost(url);

        List<NameValuePair> value = new ArrayList<NameValuePair>();

        value.add(new BasicNameValuePair("facebook", createJsonFormatDataString()));

        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(value);

        request.setEntity(entity);

        HttpResponse res = client.execute(request);

        String[] status_String=res.getStatusLine().toString().trim().split(" ");

        if(status_String[1].equals("200")){
            isDataSend= true;
        }


    } catch (Exception e) {
        System.out.println("Exp=" + e);
    }
    return isDataSend;
}

Or is there any article i can refer to understand how its done ?

Upvotes: 1

Views: 1236

Answers (1)

Selvin
Selvin

Reputation: 6797

if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
    String bufstring = EntityUtils.toString(res.getEntity(), "UTF-8");
    isDataSend= true;
}

response data will be in bufstring

EDIT:

just replace your code(bellow) with my

    String[] status_String=res.getStatusLine().toString().trim().split(" ");

    if(status_String[1].equals("200")){
        isDataSend= true;
    }

Upvotes: 3

Related Questions