1

I want to get response from the httppost request. I get the network response like 200,405,404 but i don't get the value which is coming from server. I am trying a lot but i don't get response. Please help...

My code is below-

private void UploadPost() {

        SharedPreferences sharedPreferences1 = getSharedPreferences("DATA", Context.MODE_PRIVATE);
        String ID = sharedPreferences1.getString("id", "");
        @SuppressWarnings("deprecation")
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(Url.addOffer_url);

        Log.e("uploadFile", "Source File Path " + picturePath);
        File sourceFile1 = new File(picturePath);
        if (!sourceFile1.isFile()) {
            Log.e("uploadFile", "Source File Does not exist");
            imgUploadStatus = "Source File Does not exist";
        }


        try {
            AndroidMultiPartEntity entity = new AndroidMultiPartEntity();
            File sourceFile = new File(picturePath);
            MultipartEntity entity1 = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);
            // Adding file data to http body


            entity.addPart("retailer_id", new StringBody(ID));
            entity.addPart("title", new StringBody(addoffertitle));
            entity.addPart("description", new StringBody(addofferdesc));
            entity.addPart("keyword", new StringBody(addofferkeyword));
            entity.addPart("offer_id", new StringBody(OfferListing_Id));
           // entity.addPart("payment_status",new StringBody(paymentStatus));
            // if(!picturePath.equals(""))
            entity.addPart("offer_image", new FileBody(sourceFile));
           /* else
                entity.addPart("old_pic",new StringBody(Image_Path));*/
            httppost.setEntity(entity);

            Log.d("httppost success", "httppost");

            //Run a api for net conn check
            try {

                String responseString= new String();
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity8 = response.getEntity();
                if(entity8 !=null){
                    responseString = EntityUtils.toString(entity8, "UTF-8");
                    System.out.println("Response body: " + responseString);
                }



                statusCode = 200;

            } catch (FileNotFoundException e) {
                Log.e("log_tag1", "Error FileNotFoundException new service" + e.toString());
                result = "FileNotFoundException";
            } catch (SocketTimeoutException e) {
                Log.e("log_tag2", "SocketTimeoutException new service " + e.toString());
                result = "SocketTimeoutException";
            } catch (Exception e) {
                Log.e("log_tag3", "Error converting OtherException new service " + e.toString());
                result = "OtherException";
            }


            if (statusCode == 200) {
                // Server response
                responseString = "success";



                Log.e("complete success", "Response from server: " + responseString);
            } else if (statusCode == 404) {
                responseString = "page not found";


                Log.e("complete page not found", "Response from server: " + responseString);

            } else if (statusCode == 405) {
                responseString = "no net";


                Log.e("complete no net", "Response from server: " + responseString);

            } else {
                responseString = "other";


                Log.e("complete other", "Response from server: " + responseString);

            }

        } catch (Exception e) {
            responseString = e.toString();
            responseString = "other";
            Log.e("complete", "Response from server: " + responseString);

        }

    }

I want to response from the httppost.i get the network response but i don't get the value which is coming from server.I am trying a lot but i don't get response.Please help...

Soumen Das
  • 1,292
  • 17
  • 12
  • What network response code are you getting? Is it 200, 400, or what is it? – ishmaelMakitla Apr 29 '16 at 07:56
  • 1
    hi thanks for your reply. @ ishmaelMakitla i am getting 200 response from the server i want to get json value. – Soumen Das Apr 29 '16 at 07:57
  • `System.out.println("Response: " + s);`. That should be `System.out.println("Response: " + s.toString());`. What does it print? That is not a value the server returns but a text. Consisting of lines. A page. Probably a html page or a json text. – greenapps Apr 29 '16 at 08:30
  • `s = s.append(sResponse);`. That should be `s = s.append(sResponse+"\n");`. – greenapps Apr 29 '16 at 08:33
  • `statusCode = 200;` ??????? You better remove that. Add the statement where you determine it. – greenapps Apr 29 '16 at 08:36
  • Hi @greenapps thanks for your reply i add your code but i don't get any response. – Soumen Das Apr 29 '16 at 09:08
  • If you have made changes to the code, please update the code on your question so that we get an idea of what did/did not work. I'd also like to suggest that you clean up the code a bit and leave only the code that concerns sending request and receiving response - just so we can get you to a point where the response is received. – ishmaelMakitla Apr 29 '16 at 09:15
  • Hello @ishmaelMakitla i have updated my code please check it.i can't get any value from the server. – Soumen Das Apr 29 '16 at 09:26
  • Just to be sure, Please confirm what status code you are actually getting - please add the lines: `int status = response.getStatusLine().getStatusCode(); Log.i("HttpPostRequest", "Response StatusCode = "+status)` **before** `HttpEntity entity8 = response.getEntity();` and tell us what is the actual status code... – ishmaelMakitla Apr 29 '16 at 10:07

2 Answers2

0

Try using EntityUtils instead of the BufferedReader. For instance, something like:

String responseString= new String();
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if(entity !=null){
  responseString = EntityUtils.toString(entity, "UTF-8");
}

Look at the selected answer here - it shows how to get response body for a 400-HTTP response. You can also look at this example. If you are working with JSON payload, perhaps you might want to consider using Volley - here are some examples for PUT, POST, and GET requests using Volley

Community
  • 1
  • 1
ishmaelMakitla
  • 3,784
  • 3
  • 26
  • 32
  • Hi @ishmaelMakitla, cann't get response without volley?actually need to get response against server hit? follow this link http://www.androidhive.info/2014/12/android-uploading-camera-image-video-to-server-with-progress-bar/ – Suman Apr 29 '16 at 08:12
  • You can get response without using Volley, I only suggested Volley as I find it easier to use - but do try the suggested answer and let me know if you are still not getting any response. – ishmaelMakitla Apr 29 '16 at 08:26
  • Sorry @ishmaelMakitla i am not getting any response – Soumen Das Apr 29 '16 at 08:38
  • Does the data you are posting get saved on the server, and does the server send you back the response? – ishmaelMakitla Apr 29 '16 at 08:48
  • @ishmaelMakitla, posted value saved into server and i get 200. i can't get json data. – Suman Apr 29 '16 at 09:17
0

You can try this

 InputStream inputStream = httpResponse.getEntity().getContent();
 InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
 BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
 StringBuilder stringBuilder = new StringBuilder();
 String bufferedStrChunk = null;
 while((bufferedStrChunk = bufferedReader.readLine()) != null){
        stringBuilder.append(bufferedStrChunk);
 }

Instead of

HttpEntity entity8 = response.getEntity();
                if(entity8 !=null){
                    responseString = EntityUtils.toString(entity8, "UTF-8");
                    System.out.println("Response body: " + responseString);
                }
mubeen
  • 813
  • 2
  • 18
  • 39