) 다음 코드는 저에게 적합하지 않습니다. 내가 잘못 가고 당신이 전화 나 ...JSON GSON (http :
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;
공용 클래스 데이터 객체는 {
@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의 문제점은 무엇입니까?
내가 그것을 역 직렬화해야합니까를? – user533844
잘못된 정보에 대한 자세한 정보를 얻을 수 있습니까? 해시 맵이 데이터로 채워져 있습니까? 'obj'는 데이터로 가득 차 있습니까? for 루프가 호출되거나,'obj.length' null 또는 0입니까? – Woody
FileNotFoundException 및 JsonParseException – user533844