编辑完全重新处理问题以便更好地理解
我必须http://api.bf3stats.com/pc/player/
使用 2 个 POST 参数查询给定的 url:'player'(用于玩家名称)和 'opt'(用于选项)。我已经在http://www.requestmaker.com/上使用以下数据对其进行了测试:player=Zer0conf&opt=all
. 我得到了正确的 JSON 响应(以为我不知道他们的网站如何执行查询,我猜是 php)。现在我正在尝试在 Android 中做同样的事情:
private StringBuilder inputStreamToString(InputStream is) {
//this method converts an inputStream to String representation
String line = "";
StringBuilder total = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return total;
}
这就是我提出请求的方式:
public void postData(String url, String name) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
//qname is a String containing a correct player name
nameValuePairs.add(new BasicNameValuePair("player", qname));
nameValuePairs.add(new BasicNameValuePair("opt", "all"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
//test is just a string to check the result, which should be in JSON format
test = inputStreamToString(response.getEntity().getContent())
.toString();
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
}
我在“测试”字符串中得到的不是 JSON,而是某些 bf3stats 页面的完整 HTML 标记。我的请求可能有什么问题?