Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid-Json parser not able to display data retrieved from back end
    text
    copied!<p>I am new to android. I am implementing a project in which I want to get the data from the back end and display on my android screen.</p> <p>The Json I am trying to call is:-</p> <pre><code>[{"admin":null,"card_no":"8789","created_at":"2013-04-09T12:55:54Z","deleted":0,"email":"dfds@fgfd.com","entered_by":null,"first_name":"Gajanan","id":8,"last_name":"Bhat","last_updated_by":null,"middle_name":"","mobile":87981,"updated_at":"2013-04-13T05:26:25Z","user_type_id":null},{"admin":{"created_at":"2013-04-10T09:02:00Z","deleted":0,"designation":"Sr software Engineer","email":"admin@qwe.com","first_name":"Chiron","id":1,"last_name":"Synergies","middle_name":"Sr software Engineer","office_phone":"98789765","super_admin":false,"updated_at":"2013-04-10T12:03:04Z","username":"Admin"},"card_no":"66","created_at":"2013-04-08T09:47:15Z","deleted":0,"email":"rajaarun1991","entered_by":1,"first_name":"Arun","id":1,"last_name":"Raja\n","last_updated_by":1,"middle_name":"Nagaraj","mobile":941,"updated_at":"2013-04-08T09:47:15Z","user_type_id":1}] </code></pre> <p>My JsonParser.java is as follows:-</p> <pre><code>package com.example.library; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.StatusLine; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.util.Log; import android.util.Log; public class JSONParser { static InputStream is = null; static JSONArray jarray = null; static String json = ""; // constructor public JSONParser() { } public JSONArray getJSONFromUrl(String url) { StringBuilder builder = new StringBuilder(); HttpClient client = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); try { HttpResponse response = client.execute(httpGet); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode == 200) { HttpEntity entity = response.getEntity(); InputStream content = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(content)); String line; while ((line = reader.readLine()) != null) { builder.append(line); } } else { Log.e("==&gt;", "Failed to download file"); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // try parse the string to a JSON object try { jarray = new JSONArray( builder.toString()); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } // return JSON String return jarray; } } /* public void writeJSON() { JSONObject object = new JSONObject(); try { object.put("name", ""); object.put("score", new Integer(200)); object.put("current", new Double(152.32)); object.put("nickname", "Programmer"); } catch (JSONException e) { e.printStackTrace(); } System.out.println(object); } */ </code></pre> <p>And my activity.java is as follows:-</p> <pre><code>package com.example.library; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.app.ListActivity; import android.app.ProgressDialog; import android.content.Context; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleAdapter; public class SecondActivity extends Activity { private Context context; private static String url = "http://192.168.0.100:3000/users.json"; private static final String TAG_ID = "id"; private static final String TAG_FIRST_NAME = "first_name"; private static final String TAG_MIDDLE_NAME = "middle_name"; private static final String TAG_LAST_NAME = "last_name"; // private static final String TAG_POINTS = "experiencePoints"; ArrayList&lt;HashMap&lt;String, String&gt;&gt; jsonlist = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); ListView lv ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new ProgressTask(SecondActivity.this).execute(); } private class ProgressTask extends AsyncTask&lt;String, Void, Boolean&gt; { private ProgressDialog dialog; public ProgressTask(SecondActivity secondActivity) { Log.i("1", "Called"); context = secondActivity; dialog = new ProgressDialog(context); } /** progress dialog to show user that the backup is processing. */ /** application context. */ private Context context; protected void onPreExecute() { this.dialog.setMessage("Progress start"); this.dialog.show(); } @Override protected void onPostExecute(final Boolean success) { if (dialog.isShowing()) { dialog.dismiss(); } ListAdapter adapter = new SimpleAdapter(context, jsonlist, R.layout.list_item, new String[] { TAG_ID, TAG_FIRST_NAME, TAG_MIDDLE_NAME, TAG_LAST_NAME }, new int[] { R.id.id, R.id.first_name, R.id.middle_name, R.id.last_name }); setListAdapter(adapter); // selecting single ListView item lv = getListView(); } private void setListAdapter(ListAdapter adapter) { // TODO Auto-generated method stub } protected Boolean doInBackground(final String... args) { JSONParser jParser = new JSONParser(); // getting JSON string from URL JSONArray json = jParser.getJSONFromUrl(url); for (int i = 0; i &lt; json.length(); i++) { try { JSONObject c = json.getJSONObject(i); String id = c.getString(TAG_ID); String first_name = c.getString(TAG_FIRST_NAME); String middle_name = c.getString(TAG_MIDDLE_NAME); String last_name = c.getString(TAG_LAST_NAME); HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); // adding each child node to HashMap key =&gt; value map.put(TAG_ID, id); map.put(TAG_FIRST_NAME, first_name); map.put(TAG_MIDDLE_NAME, middle_name); map.put(TAG_LAST_NAME, last_name); jsonlist.add(map); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return null; } } public ListView getListView() { // TODO Auto-generated method stub return null; } } </code></pre> <p><img src="https://i.stack.imgur.com/14gla.jpg" alt="Users screen Not giving/showing any output"></p> <p>I am not able to show anything on the emulators for users,whereas I am able to fetch the data from the server</p> <p>Actually in the project I want to display all the users present in the library.I am accessing the server which is on Ubuntu.</p> <p>The following message is displayed on the console(Terminal):-</p> <pre><code>Started GET "/users.json" for 192.168.0.104 at 2013-05-05 22:05:01 -0700 Processing by UsersController#index as JSON User Load (0.4ms) SELECT "users".* FROM "users" WHERE (users.deleted = 0) ORDER BY users.id DESC Admin Load (0.3ms) SELECT "admins".* FROM "admins" WHERE "admins"."id" = 1 AND (admins.deleted = 0) ORDER BY admins.id DESC LIMIT 1 Completed 200 OK in 4ms (Views: 2.1ms | ActiveRecord: 0.6ms) [2013-05-05 22:05:01] WARN Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true </code></pre> <p>However i am not able to display the data on the android screen/emulator.</p> <p>Please help me with this.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload