Sanat Pandey
Sanat Pandey

Reputation: 4103

Response from json post

I have a question that when we want to post a json request to a server it always returns org.apache.http.conn.EofSensorInputStream@44f4a1d0 when we want to print the response, so what is the means of this response?

Code:

  HttpClient client = new DefaultHttpClient();
                    HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
                    HttpResponse response;
                    JSONObject json = new JSONObject();
                    try{
                        HttpPost post = new HttpPost("http://122.180.114.68/eqixmobile/siteservice/um/ibx");
                        json.put("username", ed_usrName.getText().toString());
                        json.put("password", ed_pass.getText().toString());
                        post.setHeader("Content-Type", "application/json");
                        post.setHeader("Accept", "application/json");
                        StringEntity se = new StringEntity( "credentials: " + json.toString());  
                        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                        post.setEntity(se);
                        response = client.execute(post);
                        /*Checking response */
                        if(response!=null){
                            InputStream in = response.getEntity().getContent(); 
                            //String str = response.getEntity().getContent().toString();//Get the data in the entity
                            System.out.println("This is service response:"+in);
                    }
                        else
                        {
                             System.out.println("This is no any service response:");
                        }
                    }
                    catch(Exception e){
                        e.printStackTrace();
                        //createDialog("Error", "Cannot Estabilish Connection");
                    }

Upvotes: 1

Views: 2753

Answers (2)

user3683874
user3683874

Reputation: 51

I recommend to use this methods to get json response

For Get Method :

public class HttpManager {

public static String getData(String uri) {

    BufferedReader reader = null;

    try {
        URL url = new URL(uri);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();

        StringBuilder sb = new StringBuilder();
        reader = new BufferedReader(new InputStreamReader(con.getInputStream()));

        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }

        return sb.toString();

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    }

}

}

For Post Method :

public class HttpRequest {
public static String getData(String uri, String params) {

    BufferedReader reader = null;

    try {
        URL url = new URL(uri);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("POST");
        con.setDoOutput(true);
        OutputStreamWriter writer = new OutputStreamWriter(
                con.getOutputStream());
        writer.write(params);
        writer.flush();

        StringBuilder sb = new StringBuilder();
        reader = new BufferedReader(new InputStreamReader(
                con.getInputStream()));

        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }

        return sb.toString();

    } catch (Exception e) {
        e.printStackTrace();
        Log.i("exception", "" + e.getMessage());
        return null;
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    }

}

}

Upvotes: 0

user370305
user370305

Reputation: 109257

The response you get from server is in inputstream so convert it in string then use it. like,

String result=null;
InputStream is = entity.getContent();
            // convert stream to string
            result = convertStreamToString(is); 
            result = result.replace("\n", "");

public static String convertStreamToString(InputStream is) throws Exception {

    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    is.close();
    return sb.toString();
}

Upvotes: 4

Related Questions