我正在尝试将2个JSON对象(文本和图像)加载到textview和imageview中。我得到文本部分出现但它与图像崩溃。我尝试了其他问题的各种答案,但无法让它发挥作用。
这是我的代码:
public class MainActivity2 extends Activity {
TextView textText;
ListView listList;
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();
private static String url = "http://54.165.116.31/api/resource";
private static final String TAG_TEXT = "text";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
oslist = new ArrayList<HashMap<String, String>>();
Button refresh_button = (Button)findViewById(R.id.refresh_button);
refresh_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new JSONParse().execute();
}
});
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity2.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
try{
new DownloadImageTask((ImageView) findViewById(R.id.text2))
.execute(url);
textText = (TextView)findViewById(R.id.text);
String text = json.getString(TAG_TEXT);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_TEXT, text);
oslist.add(map);
listList =(ListView)findViewById(R.id.list);
ListAdapter adapter = new SimpleAdapter(MainActivity2.this, oslist,
R.layout.list_item,
new String[] { TAG_TEXT }, new int[] {
R.id.text});
listList.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView textText2;
public DownloadImageTask(ImageView textText2) {
this.textText2 = textText2;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap webImage = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
webImage = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return webImage;
}
protected void onPostExecute(Bitmap result) {
textText2.setImageBitmap(result);
}
}
}
logcat的:
10-01 21:11:47.453: I/am_on_paused_called(6307): [0,com.amiephipps.thejunglegym.MainActivity]
10-01 21:11:47.552: W/Resources(6307): Preloaded drawable resource #0x1080093 (android:drawable/sym_def_app_icon) that varies with configuration!!
10-01 21:11:47.583: I/am_on_resume_called(6307): [0,com.amiephipps.thejunglegym.MainActivity2]
10-01 21:11:47.665: I/Choreographer(6307): Skipped 42 frames! The application may be doing too much work on its main thread.
10-01 21:11:51.005: I/Choreographer(6307): Skipped 54 frames! The application may be doing too much work on its main thread.
10-01 21:11:51.082: I/art(6307): Background sticky concurrent mark sweep GC freed 1555(112KB) AllocSpace objects, 0(0B) LOS objects, 1258% free, 1149KB/3MB, paused 2.122ms total 117.748ms
10-01 21:11:51.170: I/Choreographer(6307): Skipped 57 frames! The application may be doing too much work on its main thread.
10-01 21:11:51.260: I/Choreographer(6307): Skipped 41 frames! The application may be doing too much work on its main thread.
10-01 21:11:51.313: I/art(6307): Background sticky concurrent mark sweep GC freed 809(34KB) AllocSpace objects, 0(0B) LOS objects, 1256% free, 1199KB/3MB, paused 29.547ms total 78.392ms
10-01 21:11:51.399: I/Choreographer(6307): Skipped 81 frames! The application may be doing too much work on its main thread.
10-01 21:11:51.560: I/Choreographer(6307): Skipped 99 frames! The application may be doing too much work on its main thread.
10-01 21:11:51.667: I/art(6307): Background partial concurrent mark sweep GC freed 1260(53KB) AllocSpace objects, 0(0B) LOS objects, 1256% free, 1203KB/3MB, paused 914us total 300.136ms
10-01 21:11:51.697: I/Choreographer(6307): Skipped 87 frames! The application may be doing too much work on its main thread.
10-01 21:11:52.065: I/Choreographer(6307): Skipped 55 frames! The application may be doing too much work on its main thread.
10-01 21:11:52.327: D/skia(6307): --- SkImageDecoder::Factory returned null
文本和图像是来自JSON API的数据(链接在代码中),它看起来像这样:
{ “文”: “例如”, “图像”: “example.jpg”}
感谢。