簡體   English   中英

(URL)字符串不能轉換為JSONObject

[英](URL) String cannot be converted to JSONObject

更新:代碼很好。 這只是指向數據庫的錯誤指針。

我正在使用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;
        }
    }

最可能的原因是imageURL字符串以imageURL ,或者更有可能(由於您是從數據庫中將其拉出),因此已轉義為使其無法用作URL的形式。 我建議將其打印出來,然后查看該值,此時您可以取消轉義或進行一些字符替換

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM