gpt4 book ai didi

java - 如何从 HttpPost 获取响应

转载 作者:太空宇宙 更新时间:2023-11-04 12:39:50 26 4
gpt4 key购买 nike

我想从 httppost 请求中获取响应。我得到像 200,405,404 这样的网络响应,但我没有得到来自服务器的值。我尝试了很多,但没有得到回应。请帮忙...

我的代码如下-

私有(private)无效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);

}

}

我想从httppost响应。我得到网络响应,但我没有得到来自服务器的值。我尝试了很多,但没有得到响应。请帮助...

最佳答案

尝试使用EntityUtils而不是BufferedReader。例如,类似:

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

查看所选答案 here - 它展示了如何获取 400-HTTP 响应的响应正文。您还可以查看this example 。如果您正在使用 JSON 有效负载,也许您可​​能需要考虑使用 Volley - here are some examples for PUT, POST, and GET requests using Volley

关于java - 如何从 HttpPost 获取响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36932423/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com