我已经成功parsed JSON
并获得了两个对象的价值,例如:图片, AsyncTask 中的句点,但在onPostExecute()
方法中我得到了value
仅last json object
。
这里我只得到句号的最后一个对象值:
delay, period); // getting last object's period value only
可能是什么原因,为什么我在onPostExecute()方法中只得到句点对象的最后一个对象值
MainActivity.java:
public class MainActivity extends Activity {
ArrayList<String> actorsList;
ViewFlipper viewFlipper;
Handler mHandler ;
Timer timer;
Runnable mUpdateResults;
int delay = 500;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
actorsList = new ArrayList<String>();
// execute AsyncTask
new JSONAsyncTask().execute("http://localhost/images.txt");
// handler to set duration and to upate animation
mHandler = new Handler();
// Create runnable for posting
mUpdateResults = new Runnable() {
public void run() {
viewFlipper.showNext(); // showNext() method
}
};
timer = new Timer();
}
// AsyncTask to get data from server
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
int period;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(String... urls) {
try {
//------------------>>
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("images");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
actorsList.add(object.getString("image"));
Log.d("image: ", object.getString("image"));
try {
period = Integer.parseInt(object.getString("period").toString());
Log.d("period: ", object.getString("period"));
} catch (NumberFormatException nfe) {
// TODO: handle exception
System.out.print("could not parse:" + nfe);
}
}
return true;
}
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
if(result == false)
{
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
else
{
for(int i=0; i<actorsList.size(); i++)
{
ImageView image = new ImageView(getApplicationContext());
Picasso.with(MainActivity.this)
.load(actorsList.get(i).toString())
.placeholder(R.drawable.ic_launcher)
.error(R.drawable.ic_launcher)
.into(image);
viewFlipper.addView(image); // addView(..) method
}
Log.d("period:post- ", String.valueOf(period));
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
mHandler.post(mUpdateResults);
}
}, delay, period); // getting last object's period value only
}
}
}
}
logcat的:
10-10 11:46:52.527: D/image:(542): http://localhost/image_01.jpg
10-10 11:46:52.536: D/period:(542): 50000
10-10 11:46:52.536: D/image:(542): http://localhost/image_05.jpg
10-10 11:46:52.536: D/period:(542): 5000
10-10 11:46:52.536: D/image:(542): http://localhost/image_06.jpg
10-10 11:46:52.536: D/period:(542): 5000
10-10 11:46:52.546: D/image:(542): http://localhost/image_07.jpg
10-10 11:46:52.546: D/period:(542): 25000
10-10 11:46:52.546: D/image:(542): http://localhost/image_08.jpg
10-10 11:46:52.546: D/period:(542): 5000
10-10 11:46:52.546: D/image:(542): http://localhost/image_02.jpg
10-10 11:46:52.556: D/period:(542): 5000
10-10 11:46:52.556: D/image:(542): http://localhost/image_03.jpg
10-10 11:46:52.556: D/period:(542): 1000
10-10 11:46:52.556: D/image:(542): http://localhost/image_04.jpg
10-10 11:46:52.566: D/period:(542): 1000
10-10 11:46:52.616: D/period:post-(542): 1000
答案 0 :(得分:0)
因为在这个循环中:
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
actorsList.add(object.getString("image"));
Log.d("image: ", object.getString("image"));
try {
period = Integer.parseInt(object.getString("period").toString());
Log.d("period: ", object.getString("period"));
} catch (NumberFormatException nfe) {
// TODO: handle exception
System.out.print("could not parse:" + nfe);
}
}
完成后,period
等于您解析的最后json
。
public class MainActivity extends Activity {
ArrayList<String> actorsList;
ViewFlipper viewFlipper;
Handler mHandler ;
Timer timer;
List<Integer> periodsList;
Runnable mUpdateResults;
int delay = 500;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
periodsList=new ArrayList<Integer>();
viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
actorsList = new ArrayList<String>();
// execute AsyncTask
new JSONAsyncTask().execute("http://localhost/images.txt");
// handler to set duration and to upate animation
mHandler = new Handler();
// Create runnable for posting
mUpdateResults = new Runnable() {
public void run() {
viewFlipper.showNext(); // showNext() method
}
};
timer = new Timer();
}
// AsyncTask to get data from server
class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {
int period;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(String... urls) {
try {
//------------------>>
HttpGet httppost = new HttpGet(urls[0]);
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
// StatusLine stat = response.getStatusLine();
int status = response.getStatusLine().getStatusCode();
if (status == 200) {
HttpEntity entity = response.getEntity();
String data = EntityUtils.toString(entity);
JSONObject jsono = new JSONObject(data);
JSONArray jarray = jsono.getJSONArray("images");
for (int i = 0; i < jarray.length(); i++) {
JSONObject object = jarray.getJSONObject(i);
actorsList.add(object.getString("image"));
Log.d("image: ", object.getString("image"));
try {
period = Integer.parseInt(object.getString("period").toString());
periodsList.add(Integer.parseInt(object.getString("period").toString()));
Log.d("period: ", object.getString("period"));
} catch (NumberFormatException nfe) {
// TODO: handle exception
System.out.print("could not parse:" + nfe);
}
}
return true;
}
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
protected void onPostExecute(Boolean result) {
if(result == false)
{
Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
}
else
{
for(int i=0; i<actorsList.size(); i++)
{
ImageView image = new ImageView(getApplicationContext());
Picasso.with(MainActivity.this)
.load(actorsList.get(i).toString())
.placeholder(R.drawable.ic_launcher)
.error(R.drawable.ic_launcher)
.into(image);
viewFlipper.addView(image); // addView(..) method
}
int mDelay = 0;
for(int i=0; i<periodsList.size(); i++)
{
Log.d("period:post- ", String.valueOf(periodsList.get(i)));
timer.schedule(new TimerTask() {
public void run() {
mHandler.post(mUpdateResults);
}
}, mDelay);
mDelay = mDelay + periodsList.get(i);
}
}
}
}
}
答案 1 :(得分:0)
你需要另一个Arralist。
在此列表中,您只需添加句点(就像您对actorsList所做的那样)
mainclass add
ArrayList<Integer> periods;
onCreate()中的
periods=new ArrayList<Integer>();
在doInBackground中
try {
periods.add(Integer.parseInt(object.getString("period").toString()));
Log.d("period: ", object.getString("period"));
} catch (NumberFormatException nfe) {
System.out.print("could not parse:" + nfe);
}