EDIT całkowicie ponownie pracuje na pytanie dla lepszego zrozumieniaexectuing HTTP POST zwraca HTML zamiast JSON
muszę zapytać Podany URL http://api.bf3stats.com/pc/player/
z 2 parametrami POST: 'player' (o nazwie Player) i " opt "(dla opcji). Przetestowałem to na http://www.requestmaker.com/ z następującymi danymi: player=Zer0conf&opt=all
. Otrzymuję poprawną odpowiedź JSON (myślę, że nie wiem, jak ich witryna wykonuje kwerendę, domyślam się, że to php). Teraz staram się zrobić to samo w Androidzie:
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;
}
i tak robię żądanie:
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) {
}
}
Co dostaję w „teście” String nie jest JSON, ale pełna HTML markup jakiejś stronie bf3stats. Jakieś pomysły co może być nie tak z moją prośbę?
sprawdzić, czy odpowiedź [tutaj] (http://stackoverflow.com/questions/9805300/using-httpget-returning-complete-html-code/9813473#9813473) pomocy. – yorkw