【发布时间】:2014-04-27 20:47:11
【问题描述】:
我将 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 参数对应的值?
【问题讨论】: