2

不正な URL の例外を解決しようとしましたが、うまくいきませんでした。Android の不正な例外に関連するほぼすべての投稿を検索しましたが、問題は解決しません。JSON 解析をスキップし、imageID[]= {" http://google.com/images/Ranipuram.jpg "," http://google.com/images/Ranipuramと同じ Java クラスで imagearray を宣言すると機能します。 .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 を二重引用符で囲む必要はありません。これらの二重引用符を削除したところ、機能するようになりました。ありがとうアムリヤ・カレ。

4

0 に答える 0