EDIT komplett neu arbeiten, um die Frage zum besseren Verständnisexectuing http POST gibt HTML anstelle von JSON
Ich habe die angegebene URL http://api.bf3stats.com/pc/player/
mit 2 POST-Parameter abzufragen: ‚Spieler‘ (für Spielernamen) und ' opt '(für Optionen). Ich habe es auf http://www.requestmaker.com/ mit folgenden Daten getestet: player=Zer0conf&opt=all
. Ich bekomme eine korrekte JSON-Antwort (dachte ich weiß nicht, wie ihre Website die Abfrage durchführt, ich denke, es ist PHP). Jetzt versuche ich, das gleiche in Android zu tun:
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;
}
und das ist, wie ich den Antrag stellen:
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) {
}
}
Was ich bin immer in dem ‚Test“ String ist nicht JSON, aber die komplette HTML-Markup einiger bf3stats Seite. Irgendwelche Ideen, was mit meiner Anfrage falsch sein könnte?
wenn die Antwort [hier] (http://stackoverflow.com/questions/9805300/using-httpget-returning-complete-html-code/9813473#9813473) Hilfe. – yorkw