HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://my.server:8080/android/service.php");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("action", "getjson"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
service.php
generates a json string. How would i fetch it from my response
? Btw; I've icluded the GSON
library, can i make use of any methods in it perhaps?
Solutions similar to this one looks pretty ugly, imo: best way to handle json from httpresponse android
There much be better ways, right?
Any help is appreciated, thanks
Update:
String json = EntityUtils.toString(response.getEntity());
seems to do the trick. There is just one small issue: The string is wrapped with brackets []
. Should i remove them manually? They are generated by php:s json_encode()
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…