嗨,我从 json 解析图像时遇到问题。我可以显示来自 json 的 textview,但未显示图像。
这里的代码
package com.testimage;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;
public class DetailPeta extends Activity {
ArrayList<HashMap<String, String>> dataMap = new ArrayList<HashMap<String, String>>();
private ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
JSONArray str_json = null;
Koneksi lo_Koneksi = new Koneksi();
String isi = lo_Koneksi.isi_koneksi();
String link_url = isi + "detail.php";
TextView judul_set, keterangan_set, tanggal_set;
ImageView gambar_set;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_peta);
keterangan_set = (TextView) findViewById(R.id.keterangan);
judul_set = (TextView) findViewById(R.id.judul);
tanggal_set = (TextView) findViewById(R.id.tanggal);
gambar_set = (ImageView) findViewById(R.id.gambar);
Bundle b = getIntent().getExtras();
String nama = b.getString("nama");
link_url = isi + "detail.php?nama="+nama;
new getListInfo().execute();
}
class getListInfo extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(DetailPeta.this);
pDialog.setMessage("Menghubungkan ke server...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
protected String doInBackground(String... args) {
JSONObject json = jParser.AmbilJson(link_url);
try {
str_json = json.getJSONArray("info");
for(int i = 0; i < str_json.length(); i++)
{
JSONObject ar = str_json.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("nama", ar.getString("nama"));
map.put("lat", ar.getString("lat"));
map.put("lng", ar.getString("lng"));
map.put("Tanggal", ar.getString("Tanggal"));
map.put("deskrpsi", ar.getString("deskripsi"));
map.put("gambar", ar.getString("gambar"));
dataMap.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
for (int i = 0; i < dataMap.size(); i++)
{
HashMap<String, String> map = new HashMap<String, String>();
map = dataMap.get(i);
judul_set.setText(map.get("nama"));
keterangan_set.setText(map.get("deskrpsi"));
tanggal_set.setText(map.get("Tanggal"));
new DownloadImagesTask().execute(isi+"detil.php"+map.get("gambar"));
}
}
});
}
}
public class DownloadImagesTask extends AsyncTask<String, Void, Bitmap> {
@Override
protected Bitmap doInBackground(String... urls) {
return download_Image(urls[0]);
}
@Override
protected void onPostExecute(Bitmap result) {
gambar_set.setImageBitmap(result);
}
private Bitmap download_Image(String url) {
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e("Hub","Error getting the image from server : " + e.getMessage().toString());
}
return bm;
}
}
}
我可以解析来自 nama 和 keterangan 的文本,但未显示图像。
display_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:orientation="vertical" >
<TextView android:text=""
android:layout_height="wrap_content"
android:id="@+id/judul"
android:textSize="20sp"
android:textColor="#000"
android:layout_width="fill_parent" >
</TextView>
<ImageView
android:layout_height="200dp"
android:id="@+id/gambar"
android:layout_width="fill_parent" >
</ImageView>
<TextView android:text=""
android:layout_height="wrap_content"
android:id="@+id/keterangan"
android:textSize="14sp"
android:textColor="#000"
android:layout_width="fill_parent" >
</TextView>
</LinearLayout>
</ScrollView>
</LinearLayout>
请帮我解决这个问题