Voglio creare un'app per Android per il mio sito web wordpress utilizzando il plug-in wp-api. come posso inviare HttpRequest (GET) e ricevere risposta in Json?come inviare HttpRequest e ottenere la risposta Json in Android?
risposta
Prova sotto codice per ottenere JSON da un URL
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget= new HttpGet(URL);
HttpResponse response = httpclient.execute(httpget);
if(response.getStatusLine().getStatusCode()==200){
String server_response = EntityUtils.toString(response.getEntity());
Log.i("Server response", server_response);
} else {
Log.i("Server response", "Failed to get server response");
}
Dove trovare HttpClient? Che pacchetto devo includere? – Tarion
@Tarion aggiungi semplicemente" useLibrary "org.apache.http.legacy'' nel file build.gradle a livello di app in' android' blocco sopra 'defaultConfig'. –
try {
String line, newjson = "";
URL urls = new URL(url);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(urls.openStream(), "UTF-8"))) {
while ((line = reader.readLine()) != null) {
newjson += line;
// System.out.println(line);
}
// System.out.println(newjson);
String json = newjson.toString();
JSONObject jObj = new JSONObject(json);
}
} catch (Exception e) {
e.printStackTrace();
}
Utilizzare questa funzione per ottenere JSON dall'URL.
public static JSONObject getJSONObjectFromURL(String urlString) throws IOException, JSONException {
HttpURLConnection urlConnection = null;
URL url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(10000 /* milliseconds */);
urlConnection.setConnectTimeout(15000 /* milliseconds */);
urlConnection.setDoOutput(true);
urlConnection.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
String jsonString = sb.toString();
System.out.println("JSON: " + jsonString);
return new JSONObject(jsonString);
}
Non dimenticare di aggiungere il permesso a Internet nel vostro manifesto
<uses-permission android:name="android.permission.INTERNET" />
Quindi utilizzare in questo modo:
try{
JSONObject jsonObject = getJSONObjectFromURL(urlString);
//
// Parse your json here
//
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
bella soluzione, non richiede l'importazione del client http di apache! – Jlange
Almeno due errori importanti qui. 1. 'urlConnection.setDoOutput (true);' cambia richiesta al metodo 'POST'. 2. Esegue efficacemente due richieste, 'new InputStreamReader (url.openStream()' apre 'url' ancora una volta, ignorando' urlConnection' e tutte le sue proprietà 3. 'sb.append (line +" \ n ")' costruisce un eccesso 'String'. –
- 1. Come analizzare la risposta Json in Android?
- 2. come inviare json come risposta dopo autenticazione passaporto in node.js
- 3. Come ottenere e analizzare la risposta JSON da traduce
- 4. Come inviare la richiesta POST HTTP e ricevere risposta?
- 5. Come inviare la richiesta SOAP e la risposta SOAP Parse in formato XML in Android?
- 6. Come posso inviare una risposta json nel controller symfony2
- 7. Slim - Come inviare risposta con l'intestazione "Content-Type: application/json"?
- 8. Come ottenere risposta JSON utilizzando curl
- 9. Come inviare la richiesta http e recuperare una risposta JSON C++ Boost
- 10. JQuery Mobile - inviare un modulo e ottenere risposta
- 11. android httprequest java.net.UnknownHostException
- 12. Come inviare java.util.Date in Json?
- 13. Come ottenere risposta jSON in variabile da uno script jquery
- 14. Retrofit 2 come ottenere la risposta da oggetti JSON nidificati (Android)
- 15. Come inviare JSON oggetto come parametro in retrofit 2 (Android)
- 16. Ottenere risposta JSON come parte di Rest call in Java
- 17. come inviare json object al server usando volley in android
- 18. HttpRequest la dimensione massima consentita in tomcat?
- 19. Come inviare la risposta dell'errore http in express/node js?
- 20. Come aggiungere e ignorare un campo per la risposta JSON
- 21. Prova ad ottenere risposta USSD in Android
- 22. Laravel Lumen Garantire la risposta JSON
- 23. JSON valido in risposta
- 24. Risposta JSON Zip in node.js
- 25. JSON Risposta in postPath AFHTTPClient
- 26. httprequest e dati binari in javascript
- 27. utilizzando python urllib2 per inviare richiesta POST e ottenere la risposta
- 28. come posso ottenere risposta JSON da PHP sotto il formato?
- 29. NSURLSessionDownloadTaskDelegate Risposta JSON
- 30. Come ottenere la risposta dopo il POST in AngularJS?
http://developer.android.com/training/volley/index.html –
è possibile utilizzare questo http://stackoverflow.com/a/8655039 – user1140237