EDIT completamente ri-lavorare la domanda per una migliore comprensioneexectuing http POST restituisce HTML invece di JSON
ho per interrogare il dato url http://api.bf3stats.com/pc/player/
con 2 parametri POST: 'giocatore' (per nome del giocatore) e ' opt '(per le opzioni). L'ho provato su http://www.requestmaker.com/ con i seguenti dati: player=Zer0conf&opt=all
. Sto ottenendo una risposta JSON corretta (pensavo che non so come il loro sito esegue la query, suppongo che sia php). Ora sto cercando di fare lo stesso in 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;
}
ed è così che faccio la richiesta:
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) {
}
}
Quello che sto ottenendo nel 'test" Stringa non è JSON, ma la completa HTML markup di qualche pagina bf3stats. Tutte le idee che cosa potrebbe essere sbagliato con la mia richiesta?
vedere se la risposta [qui] (http://stackoverflow.com/questions/9805300/using-httpget-returning-complete-html-code/9813473#9813473) aiuto. – yorkw