Java 在Android应用程序的Gallery中显示JSON解析URL中的图像,但出现了畸形异常
我试图解决格式错误的URL异常,但没有成功。在android中搜索了几乎所有与格式错误异常相关的帖子,但问题仍然存在。当我跳过JSON解析并在与imageID[]={“”,}相同的java类中声明imagearray时,它会起作用;当我使用system.Out.println(ImageID[0])将URL拆分并打印时,我得到了URL;但它不是一个URL 这是我的代码:Java 在Android应用程序的Gallery中显示JSON解析URL中的图像,但出现了畸形异常,java,android,json,eclipse,malformedurlexception,Java,Android,Json,Eclipse,Malformedurlexception,我试图解决格式错误的URL异常,但没有成功。在android中搜索了几乎所有与格式错误异常相关的帖子,但问题仍然存在。当我跳过JSON解析并在与imageID[]={“”,}相同的java类中声明imagearray时,它会起作用;当我使用system.Out.println(ImageID[0])将URL拆分并打印时,我得到了URL;但它不是一个URL 这是我的代码: public class GalleryActivity extends Activity { private
public class GalleryActivity extends Activity {
private static final String KEY_ROOT = DistrictActivity.dist;
static final String KEY_ID = "id";
public String KEY_IMAGE= "image_url";
public String[] imageIDs;
JSONArray contacts;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_activity);
String newString = DistrictActivity.dist;
final String URL = "http://192.168.0.105/tour/"+newString+".php";
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(URL);
try {
// Getting Array of Contacts
contacts = json.getJSONArray(KEY_ROOT);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++)
{
JSONObject c = contacts.getJSONObject(i);
String image_url =c.getString(KEY_IMAGE);
if(CustomizedListView.pos==i)
{
imageIDs=image_url.split(",");
/* System.out.println(imageIDs[0]);
*/
}
}// Storing each json item in variable
}
catch (JSONException e) {
e.printStackTrace();
}
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View v, int position,
long id) {
// Toast.makeText(getBaseContext(), "pic" + (position + 1) +
// " selected", Toast.LENGTH_SHORT).show();
// ---display the images selected---
ImageView imageView = (ImageView) findViewById(R.id.imageview1);
URL url = null;
try {
url = new URL(imageIDs[position].toString());
Bitmap bmp = null;
try {
bmp = BitmapFactory.decodeStream(url
.openConnection().getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
imageView.setImageBitmap(bmp);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context context;
private int itemBackground;
public ImageAdapter(Context c) {
context = c;
// ---setting the style---
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
// ---returns the number of images---
@Override
public int getCount() {
return imageIDs.length;
}
// ---returns the ID of an item---
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
// ---returns an ImageView view---
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
// imageView.setImageResource(imageIDs[position]);
URL url;
try {
url = new URL (imageIDs[position]);
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection()
.getInputStream());
imageView.setImageBitmap(bmp);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
imageView.setBackgroundResource(itemBackground);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return imageView;
}
}
}
公共类GalleryActivity扩展活动{
私有静态最终字符串KEY_ROOT=DistrictActivity.dist;
静态最终字符串键\u ID=“ID”;
公共字符串键\u IMAGE=“IMAGE\u url”;
公共字符串[]图像ID;
JSONArray联系人;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery_活动);
字符串newString=DistrictActivity.dist;
最终字符串URL=”http://192.168.0.105/tour/“+newString+”.php”;
JSONParser jParser=新的JSONParser();
//从URL获取JSON字符串
JSONObject json=jParser.getJSONFromUrl(URL);
试一试{
//获取联系人数组
contacts=json.getJSONArray(KEY_ROOT);
//通过所有触点循环
对于(int i=0;i
//JSON->
url在表中给出,列名为“image\u url”,数据类型为“text”。当我试图在emulator的textview中显示它们时,它显示的内容与我在数据库中给出的内容相同
//解决了问题