title: To display Images from JSON parsed URL in Gallery for an Android application, But gives MalformedURL exception
body: <p>I tried to solve the malformed URL exception, but it didnt work. Searched almost all posts relating to malformed exception in android but the problem still persists. It works when I skip JSON parsing and declares an imagearray in the same java class as imageID[]= {"<a href="http://google.com/images/Ranipuram.jpg" rel="nofollow">http://google.com/images/Ranipuram.jpg</a>","<a href="http://google.com/images/Ranipuram.jpg" rel="nofollow">http://google.com/images/Ranipuram.jpg</a>",}; I get the URL when I splitted it and printed using system.Out.println(imageIDs[0]); But it is not working as a URL.</p>
  
  <p>This is my code: </p>
  
  <pre><code>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 &lt; 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;
                  }
              }
      }
  </code></pre>
  
  <p>// JSON- ></p>
  
  <p>The url's are given in a table , the column name is "image_url", its datatype is "text". And when I tried to display them in a textview in emulator , it displays the same as I given in the database. </p>
  
  <p>//RESOLVED THE PROBLEM</p>
  
  <p>The quotes made the problem !There is no need for double quotes URL's in the database. I removed those double quotes and now its working. Thanks  Amulya Khare .</p>