Get json from HttpResponse

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 do I get it from my response ? Btw; I have included the GSON library, can I use any methods in it, maybe?

Solutions like this look pretty ugly, imo: the best way to handle json from httpresponse android

There are many better ways, right?

Any help is appreciated, thanks




Update:

 String json = EntityUtils.toString(response.getEntity()); 

seems to be doing the trick. There is only one small problem: the string is wrapped in brackets [] . Should I delete them manually? They are generated by php: s json_encode()

+22
java json android
Jun 20 2018-12-12T00:
source share
4 answers

The problem was in my php file. Removing an array of containers from a json encoded object made my java code work.

+1
Jun 20 '12 at 23:16
source

I think the problem you are facing is similar to my own, which I have encountered. If you run:

 String json_string = EntityUtils.toString (response.getEntity ());
 JSONObject temp1 = new JSONObject (json_string);

The above code throws an exception and it looks like the brackets of the JSON array are to blame. But it’s great that the JSON array is a top-level element! You just need to use JSONArray () instead of JSONObject:

 String json_string = EntityUtils.toString (response.getEntity ());
 JSONArray temp1 = new JSONArray (json_string);

So you should know if you get a JSONArray or a single dictionary, which is a JSONObject in the JSON code.

If you are used to iOS / Objective-C JSON parsing libraries, they use the same top-level element to work with json dictionaries and json arrays, so the transition to the JAVA / Android world confused me in that I have two types to handle JSON depending on the top level returned.

+18
Mar 25 '14 at 3:16
source

Using this class, you can receive JSON data either from the server or from your resource folder. It can be easily changed only to one or another. If you need an adapter, use jgilfelt created here on getHub .

 @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Bundle getArgs = this.getArguments(); String URI = getArgs.getString(KEY_URI);//OR YOU CAN HARD CODE THIS OR GET THE STRING ANYWAY YOU LIKE. new GetJSONTask().execute(URI); } class GetJSONTask extends AsyncTask<String, Integer, String> { protected String doInBackground(String... arg0) { String uri = arg0[0]; InputStream is = null; if (uri.contains("http") == true) {// Get JSON from URL try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(uri); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); BufferedReader rd = new BufferedReader(new InputStreamReader(is, "UTF-8")); while ((line = rd.readLine()) != null) { json += line; } rd.close(); return json; } catch (Exception e) { e.printStackTrace(); return null; } } else {// Get JSON from Assets Writer writer = new StringWriter(); char[] buffer = new char[1024]; try { InputStream jsonFile = getActivity().getAssets().open(uri); Reader reader = new BufferedReader(new InputStreamReader(jsonFile, "UTF-8")); int n; while ((n = reader.read(buffer)) != -1) { writer.write(buffer, 0, n); } jsonFile.close(); } catch (IOException e) { e.printStackTrace(); } json = writer.toString(); // return JSON String return json; } } @Override protected void onPostExecute(String result) { try { showData(result); } catch (JSONException e) { e.printStackTrace(); Toast.makeText(getActivity(), "something went wrong", Toast.LENGTH_SHORT).show(); } } } private void showData(String json) throws JSONException { JSONObject o = new JSONObject(json); JSONArray data = o.getJSONArray("results"); } } 
+1
Jun 20 2018-12-12T00:
source

If I return a JSON string from my web service, I usually want to return it to a JSON object, for example:

 String response = client.getResponse(); if (responseCode == 200) { JSONObject obj = new JSONObject(response); } 
0
Jun 20 2018-12-12T00:
source



All Articles