следующий код не работает для меня.Можете ли вы сказать мне, где я иду не так ...
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(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);
} catch (IOException e) {
e.printStackTrace();
}
}
}
DataObject.java
import com.google.gson.annotations.SerializedName;
открытый класс 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;
}
}
Вывод ничего не показывает.
Спасибо за ответ.Это мой файл JSON.
[{"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"}]
И эта программа работала для меня.
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);
Это означает, что он работает со строковым объектом.и JSON GSON работает.Так что не так с Http?