![](/img/trans.png)
[英]How can i make an HTTP POST request and and use the Json Response i should get in android
[英]I want to get a String return type for a http response i get for http post request i trigger
我想為觸發的http發布請求獲取http響應的String返回類型,但是我的return變量給出錯誤
“消息無法解析為變量”
隨后,我必須將該返回字符串值轉換為json字符串數據。
這是我的代碼。
public String sendPOST(String _postData) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(POST_URL);
httpPost.addHeader("User-Agent", USER_AGENT);
StringEntity entity = new StringEntity(_postData, HTTP.UTF_8);
entity.setContentType("application/json");
httpPost.setEntity(entity);
CloseableHttpResponse httpResponse;
try {
httpResponse = httpClient.execute(httpPost);
System.out.println("POST Response Status:: " + httpResponse.getStatusLine().getStatusCode());
BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = reader.readLine()) != null) {
response.append(inputLine);
String message = org.apache.commons.io.IOUtils.toString(reader);
String type = message.getClass().getName();
System.out.println(type);
System.out.println("Final : " + message);
}
reader.close();
// print result
System.out.println(response.toString());
httpClient.close();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return message;
}
因為您在循環外使用本地“消息”變量。 添加全局“ message”變量,應該可以解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.