EDIT hoàn toàn tái làm việc các câu hỏi để hiểu rõ hơnexectuing http POST trả về HTML thay vì JSON
Tôi đã để truy vấn url cho http://api.bf3stats.com/pc/player/
với 2 thông số POST: 'chơi' (đối với tên người chơi) và ' opt '(cho các tùy chọn). Tôi đã thử nghiệm nó trên http://www.requestmaker.com/ với dữ liệu sau: player=Zer0conf&opt=all
. Tôi nhận được phản hồi JSON đúng (nghĩ rằng tôi không biết trang web của họ thực hiện truy vấn như thế nào, tôi đoán đó là php). Bây giờ tôi đang cố gắng để làm điều tương tự trong 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;
}
và đó là cách tôi thực hiện yêu cầu:
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) {
}
}
gì tôi nhận được trong 'test" Chuỗi không phải là JSON, nhưng hoàn chỉnh HTML đánh dấu của một số trang bf3stats. Bất kỳ ý tưởng những gì có thể xảy ra với yêu cầu của tôi?
Xem nếu câu trả lời [ở đây] (http://stackoverflow.com/questions/9805300/using-httpget-returning-complete-html-code/9813473#9813473) giúp đỡ. – yorkw