JSON GSON from http
the following code is not working for me. Can you tel me where I am going wrong...
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<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(i开发者_JS百科nt i=0 ; i<obj.length ; i++){
HashMap<String, String> map = new HashMap<String, String>();
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();
}
}
}
DataObject.java
import com.google.gson.annotations.SerializedName;
public class DataObject {
@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;
}
}
Output does not show anything.
Thanks for replying. This is my JSON file.
[{"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"}]
And this program worked for me.
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<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();
for(int i=0 ; i<obj.length ; i++){
HashMap<String, String> map = new HashMap<String, String>();
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);
That means it's working with string object. and JSON GSON is working. So what's wrong with Http ?
With a FileNotFoundException and JsonParseException I would think it's something to do with your HTTP fetching, not JSON object creation. Set some breakpoints, and make sure the website you are fetching is returning Valid JSON (run it through a validator like this )
If that doesn't help, post back here, and we can try something else.
EDIT: Are you also sure that this line
DataObject[] obj = gson.fromJson(br, DataObject[].class)
shouldn't be this
DataObject[] obj = gson.fromJson(br, DataObject.class)
Just something else to try.
EDIT 2: Try converting the BufferedReader into a String, and passing it into the function that way. You can also print the String to make sure the HTTP fetching is working correctly. I don't know if this is the best way, but it should work.
BufferedReader br = new BufferedReader(fr);
String line;
StringBuilder result = new StringBuilder();
while ((line = br.readLine()) != null) {
result.append(line);
}
-Woody
Try using the Gson Streaming library - https://sites.google.com/site/gson/streaming
ArrayList<YourObject> yourObjects = new ArrayList<YourObject>();
URL url = new URL("http://...");
JsonReader reader = new JsonReader(new InputStreamReader(url.openStream()));
reader.beginArray();
while(reader.hasNext())
{
YourObject o = gson.fromJson(reader, YourObject.class);
yourObjects.add(o);
}
reader.endArray();
reader.close();
精彩评论