【发布时间】:2019-03-30 18:14:26
【问题描述】:
我想在 rest api 的列表视图中显示数据。起初我使用异步任务向给定的 api 发出 post 请求
public class SendRequest extends AsyncTask<String, Void, String> {
protected void onPreExecute(){}
protected String doInBackground(String... arg0) {
try{
URL url = new URL("https://staging.ineyetech.com/inweigh_dev/webservice/itinerary_list\n" +
"\n");
JSONObject postDataParams = new JSONObject();
postDataParams.put("device_type", "1");
postDataParams.put("module_type", "2");
postDataParams.put("user_time_zone", "Asia/Calcutta");
postDataParams.put("unit_type", "2");
postDataParams.put("page_no", "1");
postDataParams.put("bluetooth_battery_percentage", "25");
postDataParams.put("app_version", "Android 1.3");
postDataParams.put("device_id", "18fd7cb2716cb0e4");
postDataParams.put("device_name", "Moto G (4)");
postDataParams.put("os_version", "7.0");
postDataParams.put("device_token", "cNy2KZpIFwA");
postDataParams.put("bluetooth_device_id", "885");
postDataParams.put("itinerary_type", "2");
postDataParams.put("oauth_token", "aa0f15f302fe1c0ffe7e3655a58f56bc");
postDataParams.put("user_id", "205");
Log.e("params",postDataParams.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line="";
while((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
return sb.toString();
}
else {
return new String("false : "+responseCode);
}
}
catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
我使用异步任务完成了发布请求并从 api 获得响应。但我希望该响应显示在列表视图中。所以我就这么做了。
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
//text.setText(result);
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, airlines,
R.layout.details, new String[]{"flightname", "route",
"flightnum","time","date"}, new int[]{R.id.flightname,
R.id.route, R.id.flightnum,R.id.time,R.id.date});
lv.setAdapter(adapter);
}
}
即使我从 api 获得响应,但我无法在列表视图中看到它。需要帮忙。提前致谢
【问题讨论】:
-
你在哪里使用你从 api 得到的结果?您得到的结果在 onPostExecute 的“结果”字符串中。什么是“航空公司”?
-
airlines 是数组列表。起初,在 post execute 方法中,我使用 set text 来查看它运行良好的响应。现在我希望该数据显示在列表视图中。
-
那么,您使用的是从 api 获得的结果吗?显示到列表视图中,我认为您没有这样做..您是否将数据添加到您的航空公司? api 响应是什么样的?
-
这是我从 api {"success":1,"message":"success","current_date":"2018-10-26","data":[{" 得到的响应id":"9165","route_type":"2","flight_name":"Emirates","flight_no":"EK545","date_display":"13-03-2018","time_display":"00: 00","display_route":"MAA 到 DXB","itinerary_type":"2"},以此类推
标签: android json rest listview