我将NameValuePair参数发送到服务器上的一个php文件中,这个php文件会回传三个字符串值中的一个。
我需要用Java编写代码,通过POST将这些参数发送到PHP文件,并将php文件的回显响应保存为字符串。
到目前为止,这就是我所拥有的:
public String getStringFromUrl(String url, List<NameValuePair> params) throws IOException {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
HttpPost httpPost = new HttpPost(url);
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
String response2 = (String) response;
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return response;
}我知道我有正确的代码行来发送POST参数,但是如何读取php文件对应于给定POST参数的值呢?
发布于 2014-04-27 20:50:23
您可以这样阅读post回复:
HttpResponse response = client.execute(post);
System.out.println("Response Code : "
+ response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";请参阅此处的完整示例:
http://www.mkyong.com/java/apache-httpclient-examples/
https://stackoverflow.com/questions/23328796
复制相似问题