(URL)字符串无法转换为JSONObject

时间:2012-11-16 19:08:10

标签: java android json imageview

更新:代码很好。它只是指向数据库的错误指针。

我正在使用JSONParser方法来生成http请求以获取JSON数据并从数据库返回JSONObject。获取JSONObject并显示字符串(如名称和电子邮件)没有问题,但它不会检索图像的URL。 我没有收到任何错误,但它只是显示空字段。

但是,如果我从

开始这一行
String imageURL = directory.getString(TAG_IMG);

到这个

String imageURL = "http://mywebsite.com/images/photo1.png";

工作正常。

//URL to make request
private static final String url_veiw_directory = "http://www.myweb.com/android/include/directory_detail_me.php";

private static final String TAG_SUCCESS = "success";
private static final String TAG_DIRECTORY = "directory";
private static final String TAG_ID = "user_id";
private static final String TAG_IMG = "photo"; // Image URLs stored in the DB
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";

//Get JSONObject by httpRequest
JSONObject json = jsonParser.makeHttpRequest(url_veiw_directory, "GET", params);

Log.d("my profile", json.toString());

 if (success == 1) {

    JSONArray directoryObj = json.getJSONArray(TAG_DIRECTORY);  

    JSONObject directory = directoryObj.getJSONObject(0);

    //User Image                            
    int loader = R.drawable.loader; 

    String imageURL = directory.getString(TAG_IMG);                         
    ImageView imagePhoto = (ImageView) findViewById(R.id.photo);

    ImageLoader imgLoader = new ImageLoader(getApplicationContext());        
    imgLoader.DisplayImage(imageURL, loader, imagePhoto);

    //User Name and Email                           
    TextView txtName = (TextView) findViewById(R.id.name);
    TextView txtEmail = (TextView) findViewById(R.id.email);                            

    txtName.setText(directory.getString(TAG_NAME));
    txtEmail.setText(directory.getString(TAG_EMAIL));

//Image Loader Class

public void DisplayImage(String url, int loader, ImageView imageView)
    {
        stub_id = loader;
        imageViews.put(imageView, url);
        Bitmap bitmap=memoryCache.get(url);
        if(bitmap!=null)
            imageView.setImageBitmap(bitmap);
        else
        {
            queuePhoto(url, imageView);
            imageView.setImageResource(loader);
        }
    }

    private void queuePhoto(String url, ImageView imageView)
    {
        PhotoToLoad p=new PhotoToLoad(url, imageView);
        executorService.submit(new PhotosLoader(p));
    }

    private Bitmap getBitmap(String url)
    {
        File f=fileCache.getFile(url);

        //from SD cache
        Bitmap b = decodeFile(f);
        if(b!=null)
            return b;

        //from web
        try {
            Bitmap bitmap=null;
            URL imageUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);
            InputStream is=conn.getInputStream();
            OutputStream os = new FileOutputStream(f);
            ImageUtils.CopyStream(is, os);
            os.close();
            bitmap = decodeFile(f);
            return bitmap;
        } catch (Exception ex){
           ex.printStackTrace();
           return null;
        }
    }

1 个答案:

答案 0 :(得分:1)

最有可能的原因是imageURL字符串是以太空或更可能(因为您从数据库中提取它)它被转义为使其无法用作URL的形式。我建议将它打印出来,看看你可能无法解决它的价值或做一些字符替换

您可能会看到类似http%3A//mywebsite.com/images/photo1.png

的内容