Note that there are some explanatory texts on larger screens.

plurals
  1. POJSON GSON from http
    primarykey
    data
    text
    <p>the following code is not working for me. Can you tel me where I am going wrong...</p> <pre><code>public class HttpActivity extends Activity { /** Called when the activity is first created. */ /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listplaceholder); ListView lv= (ListView)findViewById(R.id.list); Gson gson = new Gson(); try { HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet("http://......."); HttpResponse response = httpclient.execute(httpget); BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); DataObject[] obj = gson.fromJson(br, DataObject[].class); List&lt;HashMap&lt;String, String&gt;&gt; fillMaps = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); for(int i=0 ; i&lt;obj.length ; i++){ HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); map.put("AC", obj[i].AC); map.put("Fleet", obj[i].Fleet ); map.put("SubFleet", obj[i].SubFleet ); fillMaps.add(map); } SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.main, new String[] {"AC", "Fleet", "SubFleet"}, new int[] {R.id.ac, R.id.fleet, R.id.subfleet}); lv.setAdapter(adapter); } catch (IOException e) { e.printStackTrace(); } } } </code></pre> <p>DataObject.java</p> <pre><code>import com.google.gson.annotations.SerializedName; </code></pre> <p>public class DataObject {</p> <pre><code> @SerializedName("AC") public String AC; @SerializedName("Fleet") public String Fleet; @SerializedName("SubFleet") public String SubFleet; public DataObject(String AC, String Fleet, String SubFleet) { this.AC = AC; this.Fleet = Fleet; this.SubFleet = SubFleet; } public String toString() { return this.AC + this.Fleet + this.SubFleet; } </code></pre> <p>}</p> <p>Output does not show anything.</p> <hr> <p>Thanks for replying. This is my JSON file.</p> <pre><code>[{"AC":"200","Fleet":"MD80","SubFleet":"83G"},{"AC":"201","Fleet":"MD80","SubFleet":"83G"},{"AC":"202","Fleet":"MD80","SubFleet":"83G"},{"AC":"262","Fleet":"MD80","SubFleet":"S80"},{"AC":"271","Fleet":"MD80","SubFleet":"S80"},{"AC":"278","Fleet":"MD80","SubFleet":"S80"},{"AC":"319","Fleet":"B767","SubFleet":"76ER"},{"AC":"320","Fleet":"B767","SubFleet":"76ER"},{"AC":"321","Fleet":"B767","SubFleet":"76ER"}] </code></pre> <p>And this program worked for me.</p> <pre><code>public class HttpActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listplaceholder); ListView lv= (ListView)findViewById(R.id.list); String response = "[{\"AC\":\"200\",\"Fleet\":\"MD80\",\"SubFleet\":\"83G\"}, {\"AC\":\"201\",\"Fleet\":\"MD80\",\"SubFleet\":\"83G\"}]"; Gson gson = new Gson(); DataObject[] obj = gson.fromJson(response, DataObject[].class); String str = gson.fromJson("\"ABC\"", String.class); List&lt;HashMap&lt;String, String&gt;&gt; fillMaps = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); for(int i=0 ; i&lt;obj.length ; i++){ HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;(); map.put("AC", obj[i].AC); map.put("Fleet", obj[i].Fleet ); map.put("SubFleet", obj[i].SubFleet ); fillMaps.add(map); } SimpleAdapter adapter = new SimpleAdapter(this, fillMaps, R.layout.main, new String[] {"AC", "Fleet", "SubFleet"}, new int[] {R.id.ac, R.id.fleet, R.id.subfleet}); lv.setAdapter(adapter); </code></pre> <p>That means it's working with string object. and JSON GSON is working. So what's wrong with Http ? </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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