I Googled
很多,但沒有這個機會。 現在,我有一個Inner AsyncTask
類,我想使用上層類中的返回值。 它成功地工作,因爲我已經把Log.e()
,它顯示了自己的價值,這是我的代碼:從void AsyncTask返回ArrayList
public class Consume extends AsyncTask<Void, Void, Void> {
private List<LatLng> latLngs = new ArrayList<>();
private List<ContactModel> contacts = new ArrayList<>();
InputStream inputStream = null;
String result = "";
@Override
protected Void doInBackground(Void... params) {
String URL = "http://x.x.x.x/MYWCF/Service1.svc/Json/getContact";
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
post.setEntity(new UrlEncodedFormEntity(param));
HttpResponse httpResponse = httpClient.execute(post);
HttpEntity httpEntity = httpResponse.getEntity();
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();
}
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("StringBuilding", "Error converting result " + e.toString());
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
List<ContactModel> contactModels;
List<LatLng> myLatLngs = new ArrayList<>();
try {
JSONObject object = new JSONObject(result);
JSONArray jArray = object.getJSONArray("getContactResult");
Type listType = new TypeToken<List<ContactModel>>() {
}.getType();
contactModels = new Gson().fromJson(String.valueOf(jArray), listType);
setContacts(contactModels);
setContacts(contactModels);
for(ContactModel contactModel : contactModels) {
Double latitude = Double.valueOf(contactModel.getLatitude());
Double longitude = Double.valueOf(contactModel.getLongitude());
LatLng latLong = new LatLng(latitude, longitude);
myLatLngs.add(latLong);
}
setLatLngs(myLatLngs);
Log.e("SizeOfArray", myLatLngs.size()+"");
} catch (JSONException e) {
e.printStackTrace();
}
}
public void setContacts(List<ContactModel> contacts) {
this.contacts = contacts;
}
public List<ContactModel> getContacts() {
return contacts;
}
public void setLatLngs(List<LatLng> latLngs) {
this.latLngs = latLngs;
}
public List<LatLng> getLatLngs() {
return latLngs;
}
在我Activity Class
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.osmdroid_map);
Consume consume = new Consume();
consume.execute();
當我運行該應用程序,原因行:
Log.e("SizeOfArray", myLatLngs.size()+"");
它返回我數組大小的正確INT數logcat
,但我想用在我的應用程序, 我甚至在doInBackground
方法裏做了所有的東西,甚至做了一個static
變量, 沒機會。 任何人都可以通過我的代碼提供真實樣本嗎? Thx
在'onPostExecute'中調用一個函數並將myLatLngs.size()傳遞給它,然後執行你打算做的任何事情。 – Alamri
我做到了這一點,沒有機會,我傳遞了整個數組,因爲我需要整個數組。 我做了set setContacts(ArrayListOfContactModel),然後在我的onCreate中調用如下:consume.getContacts(); 沒有運氣。 –
如果我可能會問你打算在myLatLngs.size()中使用什麼? – Alamri