我想从rest api在列表视图中显示数据。首先,我使用异步任务向给定的api发送了发布请求
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());
}
}
我使用Async任务完成了发布请求,并从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得到响应,但在列表视图中也看不到它。需要帮忙。预先感谢
答案 0 :(得分:1)
如果我遵循您的代码,则看不到要在航空公司arraylist中添加数据的任何行。 在添加适配器之前,您需要添加适配器。
在onPostExecute方法内部,放在下面的代码中-
try {
JSONObject obj = new JSONObject(result);
JsonArray airlinesArray = obj.getJSONArray("data");
for(int i = 0; i < airlinesArray.size(); i++){
JSONObject object = airlinesArray.get(i);
// do code to add data into airlines arraylist
}
} catch (Exception e) {
}
//here put your adapter setting code
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);
答案 1 :(得分:0)
在protected void onPostExecute(String result)
内,您需要创建JsonObject,然后创建航空公司的JsonArray,然后添加到airlines
数组列表中。
需要做类似的事情,
try {
JSONObject obj = new JSONObject(result);
JsonArray airlinesArray = obj.getJSONArray("data");
for(int i = 0; i < airlinesArray.size(); i++){
JSONObject object = airlinesArray.get(i);
//create airlines arraylist data type object with data and initialize variables using object.getString("s_name");
// Then insert that data into arraylist
// airlines.add(data);
}
} catch (Exception e) {
}
在此之后,将适配器添加到listView中。