我试图解决格式错误的 URL 异常,但没有奏效。搜索了几乎所有与android中格式错误的异常有关的帖子,但问题仍然存在。当我跳过 JSON 解析并在与 imageID[]= {" http://google.com/images/Ranipuram.jpg "," http://google.com/images/Ranipuram .jpg ",}; 当我拆分它并使用 system.Out.println(imageIDs[0]); 打印时,我得到了 URL 但它不能用作 URL。
这是我的代码:
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;
}
}
}
// JSON->
url 在表格中给出,列名是“image_url”,它的数据类型是“text”。当我尝试在 emulator 的 textview 中显示它们时,它的显示与我在数据库中给出的相同。
//解决了问题
引号引起了问题!数据库中不需要双引号 URL。我删除了那些双引号,现在它可以工作了。谢谢阿穆利亚·哈尔。