简体   繁体   English

如何从HttpPost获得响应

[英]How to get response from HttpPost

I want to get response from the httppost request. 我想从httppost请求中获得响应。 I get the network response like 200,405,404 but i don't get the value which is coming from server. 我得到网络响应像200,405,404但我没有得到来自服务器的价值。 I am trying a lot but i don't get response. 我尝试了很多,但我没有得到回应。 Please help... 请帮忙...

My code is below- 我的代码如下 -

private void UploadPost() { 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... 我想从httppost.i得到网络响应的响应,但我没有得到来自服务器的价值。我正在尝试很多,但我没有得到回应。请帮助...

Try using EntityUtils instead of the BufferedReader . 尝试使用EntityUtils而不是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. 这里查看所选答案 - 它显示了如何获得400-HTTP响应的响应正文。 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 如果你正在使用JSON有效负载,也许你可能想考虑使用Volley - 这里有一些使用Volley 的PUT,POST和GET请求的例子

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);
                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM