我正在尝试将ArrayList<ItemDTO> data
传递给onPostExecute
方法,但始终将null
传递给onPostExecute()
方法。我已经调试了它,你可以在下面的屏幕截图中看到doInBackground
返回带有long, lat, route, direction
值的2个元素的arrayList。我该如何解决?
GetLLRD课程:
public class GetLLRD {
Context mContext;
String json;
public void post_selected(String json, Context context) {
this.json = json;
new MyAsyncTask().execute(json);
context = this.mContext;
}
class MyAsyncTask extends AsyncTask<String, Integer, ArrayList<ItemDTO>> {
ArrayList<ItemDTO> data;
@Override
protected ArrayList<ItemDTO> doInBackground(String... params) {
.
.
.
.
try {
Gson gson = new Gson();
Type listType = new TypeToken<List<ItemDTO>>() {
}.getType();
data = gson.fromJson(sb.toString(), listType);
} catch (JsonSyntaxException e) {
e.printStackTrace();
}
for (ItemDTO itemDTO : data) {
double latitude = itemDTO.getLatitude();
double longitude = itemDTO.getLongitude();
int route = itemDTO.getRoute();
String direction = itemDTO.getDirection();
System.out.println(latitude + ", " + longitude + ", "
+ ", " + route + ", " + direction);
}
.
.
.
.
return data;
}
protected void onPostExecute(ArrayList<ItemDTO> result) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
new MyAsyncTask().execute(json);
System.out.println("The method onPostExcute() in GETLLRD class was invoked again");
}
}, 1*30 * 1000);
if (result != null) {
Intent intent = new Intent(mContext, Map.class);
intent.putExtra("list",result);
mContext.startActivity(intent);
}else{
Log.e("123", "Avoiding null pointer, the dat is null in the GETLLRD class!!!");
}
}
}
}
MapDataJSON:
public class MapDataJSON {
ArrayList<ItemDTO> items;
public MapDataJSON(ArrayList<ItemDTO> items) {
super();
this.items = items;
}
public ArrayList<ItemDTO> getItems() {
return items;
}
public void setItems(ArrayList<ItemDTO> items) {
this.items = items;
}
public static class ItemDTO implements Serializable {
private static final long serialVersionUID = 1L;
double latitude;
double longitude;
int route;
String direction;
public ItemDTO(double latitude, double longitude, int route,
String direction) {
super();
this.latitude = latitude;
this.longitude = longitude;
this.route = route;
this.direction = direction;
}
public double getLatitude() {
return latitude;
}
public double getLongitude() {
return longitude;
}
public int getRoute() {
return route;
}
public String getDirection() {
return direction;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
public void setRoute(int route) {
this.route = route;
}
public void setDirection(String direction) {
this.direction = direction;
}
}
}
JSON简单:
[
{
"latitude": 20.123456,
"longitude": 70.123456,
"route": 4,
"direction": "ABC"
},
{
"latitude": 30.123456,
"longitude": 80.123456,
"route": 9,
"direction": "DEF"
},
{
"latitude": 10.123456,
"longitude": 90.123456,
"route": 3,
"direction": "GHI"
}
]
答案 0 :(得分:0)
我还没有测试过,但您可以尝试将参数设置为onPostExecute()
到
protected void onPostExecute(List<ItemDTO> result)
而不是
protected void onPostExecute(ArrayList<ItemDTO> result)
答案 1 :(得分:0)
请在dobackground方法中尝试以下给定代码。
Gson gson = new Gson();
List<ResponseDetail> countriesResponseList = new ArrayList<ResponseDetail>();
Type collectionType = new TypeToken<List<ResponseDetail>>(){}.getType();
try {
countriesResponseList = gson.fromJson(sb.toString(), collectionType);
} catch (JsonSyntaxException e) {e.printStackTrace();} catch (JSONException e) {e.printStackTrace();}
答案 2 :(得分:0)
我在下面的代码中所做的是将整个Json
响应作为String
返回,然后将其转换为JsonArray
并循环它并提取每个对象并添加它是ArrayList
。
同样客观的不同方法。
<强>代码:强>
class MyAsyncTask extends AsyncTask<String, String, String> {
ArrayList<ItemDTO> data;
@Override
protected String doInBackground(String... params) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
try {
// Set up HTTP post
// HttpClient is more then less deprecated. Need to change to
// URLConnection
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(YourURL);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
// Read content & Log
inputStream = httpEntity.getContent();
} catch (UnsupportedEncodingException e1) {
Log.e("UnsupportedEncoding", e1.toString());
e1.printStackTrace();
} catch (ClientProtocolException e2) {
Log.e("ClientProtocolException", e2.toString());
e2.printStackTrace();
} catch (IllegalStateException e3) {
Log.e("IllegalStateException", e3.toString());
e3.printStackTrace();
} catch (IOException e4) {
Log.e("IOException", e4.toString());
e4.printStackTrace();
}
// Convert response to string using String Builder
try {
BufferedReader bReader = new BufferedReader(
new InputStreamReader(inputStream, "utf-8"), 8);
StringBuilder sBuilder = new StringBuilder();
String line = null;
while ((line = bReader.readLine()) != null) {
sBuilder.append(line + "\n");
}
inputStream.close();
result = sBuilder.toString();
} catch (Exception e) {
Log.e("StringBuildingReader",
"Error convertingt " + e.toString());
}
return result;
}
protected void onPostExecute(String result) {
JSONArray att = new JSONArray(result);
for (int i = 0; i < att.length(); i++) {
JSONObject jObject = att.getJSONObject(i);
double latitude = jObject.getDouble("latitude");
double longitude = jObject.getDouble("longitude");
int route = jObject.getInt("route");
String direction = jObject.getString("direction");
ItemDTO itme = new ItemDTO(latitude,longitude,route,direction);
YourArrayList.add(item);
}
//do what ever you need to do with your ArrayList here
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
new MyAsyncTask().execute(json);
System.out.println("The method onPostExcute() in GETLLRD class was invoked again");
}
}, 1*30 * 1000);
if (result != null) {
Intent intent = new Intent(mContext, Map.class);
intent.putExtra("list",result);
mContext.startActivity(intent);
}else{
Log.e("123", "Avoiding null pointer, the dat is null in the GETLLRD class!!!");
}
}
}
希望这对你有用。