我正在使用一个返回 JSON 作为响应的 Web 服务。我想解析响应并创建一个对象列表。我不知道如何在 android 的 UI 中显示解析的数据。用户可以从列表中选择一项(即迪拜购物中心或迪拜媒体城或迪拜节)。
public class MyLocation extends MapActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location);
initButtons();
initMapView();
initMyLocation();
jObject=getJSONFile("http://loyaltier.com/app/mobile/code/places/Maps.php");
}
private JSONObject getJSONFile(String URL) {
JSONObject jObject=null;
HttpGet httpGet=new HttpGet(URL); //http://loyaltier.com/app/mobile/code/places/Maps.php
HttpClient httpClient=new DefaultHttpClient();
HttpResponse httpResponse;
StringBuilder stringBuilder=new StringBuilder();
try{
httpResponse=httpClient.execute(httpGet);
HttpEntity httpEntity=httpResponse.getEntity();
InputStream inputStream=httpEntity.getContent();
int c;
while((c=inputStream.read())!=-1){
stringBuilder.append((char)c);
}
}catch(Exception e){
e.printStackTrace();
}
try{
jObject=new JSONObject(stringBuilder.toString());
}catch(JSONException e){
Log.e("Log_tags ", "Error parsing data "+e.toString());
}
return jObject;
}
}