我正在为Android制作照片流媒体应用。为了避免内存问题,我运行了两个AsyncTasks来下载数据。获取并将JSON对象解析为自定义对象(此处显示为:)
package edu.iastate.its.webdev.training.photostream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.util.JsonReader;
public class StringAsync extends AsyncTask<String, Integer, String>
{
@Override
protected String doInBackground(String...urls)
{
InputStream content = null;
DefaultHttpClient temp = new DefaultHttpClient();
HttpGet temp2 = new HttpGet(urls[0]);
try {
HttpResponse temp3 = temp.execute(temp2);
content = temp3.getEntity().getContent();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Reader reader = null;
try {
reader = new InputStreamReader(content, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
char[] buffer = new char[Integer.MAX_VALUE/200];
try {
reader.read(buffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new String(buffer);
}
@Override
protected void onPostExecute(String result)
{
if(result == null) throw new IllegalStateException("No result");
if(result.substring(0, 3).equals("null")) result = result.substring(4, result.length()-1);
super.onPostExecute(result);
JsonReader temp = new JsonReader(new StringReader(result));
try {
temp.beginArray();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
temp.setLenient(true);
try {
while(temp.hasNext())
{
PhotoItem tempItem = new PhotoItem("null", 0, 0, "null", "null", "null");
temp.beginObject();
temp.skipValue();
tempItem.setId(temp.nextInt());
temp.skipValue();
tempItem.setName(temp.nextString());
temp.skipValue();
tempItem.setDescription(temp.nextString());
temp.skipValue();
temp.skipValue();
temp.skipValue();
temp.skipValue();
temp.skipValue();
temp.skipValue();
temp.skipValue();
tempItem.setThumbImageURL(temp.nextString());
temp.skipValue();
temp.skipValue();
temp.skipValue();
tempItem.setFullImageURL(temp.nextString());
temp.skipValue();
tempItem.setViewsNumber(temp.nextInt());
temp.skipValue();
temp.skipValue();
temp.skipValue();
temp.skipValue();
//temp.skipValue();
temp.endObject();
MainActivity.photos.add(tempItem);
}
//temp.endArray();
temp.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
另一个从JSON中的URL下拉图像:
package edu.iastate.its.webdev.training.photostream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
public class PhotoAsync extends AsyncTask<ArrayList<PhotoItem>, Integer, Bitmap>
{
private ArrayList<PhotoItem> temp = null;
@Override
protected synchronized Bitmap doInBackground(ArrayList<PhotoItem>...idArg)
{
temp = idArg[0];
String tempURL = "null";
for(PhotoItem p : temp)
{
p.setThumbImageBM(getBitmapFromURL(p.getThumbURL()));
p.setFullImageBM(getBitmapFromURL(p.getFullURL()));
}
if(temp.size() > 0)
return temp.get(0).getLargeImage();
else
return null;
}
@Override
protected synchronized void onPostExecute(Bitmap result)
{
}
private Bitmap getBitmapFromURL(String src)
{
try {
java.net.URL url = new java.net.URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
BitmapFactory.Options option = new BitmapFactory.Options();
option.inSampleSize = 8;
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
我一直收到这个错误:
04-15 17:52:04.539: W/dalvikvm(23235): threadid=14: thread exiting with uncaught exception (group=0x417c0da0)
04-15 17:52:04.539: E/AndroidRuntime(23235): FATAL EXCEPTION: AsyncTask #4
04-15 17:52:04.539: E/AndroidRuntime(23235): Process: edu.iastate.its.webdev.training.photostream, PID: 23235
04-15 17:52:04.539: E/AndroidRuntime(23235): java.lang.RuntimeException: An error occured while executing doInBackground()
04-15 17:52:04.539: E/AndroidRuntime(23235): at android.os.AsyncTask$3.done(AsyncTask.java:300)
04-15 17:52:04.539: E/AndroidRuntime(23235): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
04-15 17:52:04.539: E/AndroidRuntime(23235): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
04-15 17:52:04.539: E/AndroidRuntime(23235): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
04-15 17:52:04.539: E/AndroidRuntime(23235): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-15 17:52:04.539: E/AndroidRuntime(23235): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-15 17:52:04.539: E/AndroidRuntime(23235): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-15 17:52:04.539: E/AndroidRuntime(23235): at java.lang.Thread.run(Thread.java:841)
04-15 17:52:04.539: E/AndroidRuntime(23235): Caused by: java.util.ConcurrentModificationException
04-15 17:52:04.539: E/AndroidRuntime(23235): at java.util.ArrayList$ArrayListIterator.next(ArrayList.java:573)
04-15 17:52:04.539: E/AndroidRuntime(23235): at edu.iastate.its.webdev.training.photostream.PhotoAsync.doInBackground(PhotoAsync.java:25)
04-15 17:52:04.539: E/AndroidRuntime(23235): at edu.iastate.its.webdev.training.photostream.PhotoAsync.doInBackground(PhotoAsync.java:1)
04-15 17:52:04.539: E/AndroidRuntime(23235): at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-15 17:52:04.539: E/AndroidRuntime(23235): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-15 17:52:04.539: E/AndroidRuntime(23235): ... 4 more
我是网络代码的新手,也是JSON的新手。任何帮助表示赞赏!
答案 0 :(得分:1)
在Java中,不允许使用foreach循环原样修改List
。这就是调用ConcurrentModificationException的原因。有关详细信息,请参阅http://docs.oracle.com/javase/6/docs/api/java/util/ConcurrentModificationException.html。
您可以使用普通的for循环并以这种方式迭代/修改。