I Googled
しかしこれは間違いありません。 今のところ、私はInner AsyncTask
クラスを持っています。私は上位クラスの戻り値を使いたいと思っています。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()
を入れている原因、それは成功し、仕事とそれが私の値を示し、ここに私のコードですライン:
Log.e("SizeOfArray", myLatLngs.size()+"");
それは私logcat
で配列サイズの正しいint型数を返しますが、私は私のアプリでそれを使用したい、 私はdoInBackground
メソッドでもすべてのものを行い、さらにstatic
という変数を作成します。 チャンスはありません。 誰かが自分のコードで実際のサンプルを与えることができますか? Thx
'onPostExecute'の中で関数を呼び出し、それにmyLatLngs.size()を渡してから、これまで何をしようとしています。 – Alamri
私はそれをしなかった、まったく、私は配列全体を渡す、私は全体の配列が必要な原因。 私はbu setContacts(ArrayListOfContactModel)を実行し、次にonCreateで呼び出されました:consume.getContacts(); 運がありません。 –
もしあなたがmyLatLngs.size()を使う予定があるのであれば? – Alamri