2015-04-06 93 views
0

我解析一個json字符串,從中我得到的圖像的網址。然後我將該URL傳遞給獲取圖像並將其顯示在imageview中的方法,但該圖像未加載並引發java.net.MalformedURLException異常。當我嘗試將圖像url直接傳遞給該方法時,它會被加載。所以我不知道錯誤在哪裏。任何幫助將不勝感激。提前致謝。我的代碼是低於圖像不加載和throwingjava.net.MalformedURLException

public class CompanyDetailActivity extends Activity { 

    ImageView coverimage; 
    ImageView profileimage; 
    TextView fullname; 
    TextView tagline; 
    TextView industry; 
    TextView teamsize; 
    TextView about; 
    TextView year; 
    TextView location; 

    String Coverimage; 
    String Profimage; 
    String Fullname; 
    String Tagline; 
    String Industry; 
    String Teamsize; 
    String About; 
    String Year; 
    String Location; 

    // Bitmap bitmap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.companydetails); 

     coverimage = (ImageView) findViewById(R.id.CoverImage); 
     profileimage = (ImageView) findViewById(R.id.ProfileImage); 
     fullname = (TextView) findViewById(R.id.FullName); 
     tagline = (TextView) findViewById(R.id.TagLine); 
     industry = (TextView) findViewById(R.id.IndustryName); 
     teamsize = (TextView) findViewById(R.id.TeamSize); 
     about = (TextView) findViewById(R.id.CompanyAbout); 
     year = (TextView) findViewById(R.id.FoundYear); 
     location = (TextView) findViewById(R.id.Location); 

     new DetailsAsynTask() 
       .execute("http://www.mygmn.com/joblink/wp-admin/admin-ajax.php?action=joblink_searchcompanies&company_id=1180"); 

     GetXMLTask task = new GetXMLTask(); 
     task.execute(Coverimage); 

    } 

    public class DetailsAsynTask extends AsyncTask<String, Void, Boolean> { 

     @Override 
     protected Boolean doInBackground(String... arg0) { 
      try { 

       HttpClient client = new DefaultHttpClient(); 
       HttpPost post = new HttpPost(arg0[0]); 

       HttpResponse response = client.execute(post); 

       int status = response.getStatusLine().getStatusCode(); 
       if (status == 200) { 
        // to get response 
        HttpEntity entity = response.getEntity(); 
        String data = EntityUtils.toString(entity); 

        JSONObject jObj = new JSONObject(data); 
        JSONObject MainObject = jObj.getJSONObject("data"); 
        CompanyDetailsModel company = new CompanyDetailsModel(); 
        Coverimage = company.setCoverImage(MainObject 
          .getString("cove_img")); 

        Profimage = company.setCompanyProfPicture(MainObject 
          .getString("company_profile_picture")); 

        Fullname = company.setCompanyFullName(MainObject 
          .getString("company_full_name")); 

        Tagline = company.setComapnyTagLine(MainObject 
          .getString("company_tagline")); 
        Industry = company.setCompanyInustry(MainObject 
          .getString("company_industry")); 
        Teamsize = company.setCompanyTeamSize(MainObject 
          .getString("company_teamsize")); 
        About = company.setCompanyAbout(MainObject 
          .getString("company_about")); 
        Year = company.setCompanyFoundYear(MainObject 
          .getString("company_foundyear")); 
        Location = company.setCompanyLocation(MainObject 
          .getString("company location")); 

        return true; 
       } 
      } catch (ClientProtocolException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      return false; 
     } 

     @Override 
     protected void onPostExecute(Boolean result) { 
      super.onPostExecute(result); 
      if (result == false) { 

      } else { 

       fullname.setText(Fullname); 
       tagline.setText(Tagline); 
       industry.setText(Industry); 
       teamsize.setText(Teamsize); 
       about.setText(About); 
       year.setText(Year); 
       location.setText(Location); 

      } 
     } 

    } 

    private class GetXMLTask extends AsyncTask<String, Void, Bitmap> { 
     @Override 
     protected Bitmap doInBackground(String... urls) { 
      Bitmap map = null; 
      for (String url : urls) { 
       map = downloadImage(url); 
      } 

      return map; 
     } 

     // Sets the Bitmap returned by doInBackground 
     @Override 
     protected void onPostExecute(Bitmap result) { 
      coverimage.setImageBitmap(result); 
     } 

     // Creates Bitmap from InputStream and returns it 
     private Bitmap downloadImage(String url) { 
      Bitmap bitmap = null; 
      InputStream stream = null; 
      BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
      bmOptions.inSampleSize = 1; 

      try { 
       stream = getHttpConnection(url); 
       bitmap = BitmapFactory.decodeStream(stream, null, bmOptions); 
       stream.close(); 
      } catch (IOException e1) { 
       e1.printStackTrace(); 
      } 
      return bitmap; 
     } 

     // Makes HttpURLConnection and returns InputStream 
     private InputStream getHttpConnection(String urlString) 
       throws IOException { 
      InputStream stream = null; 
      URL url = new URL(urlString); 
      URLConnection connection = url.openConnection(); 

      try { 
       HttpURLConnection httpConnection = (HttpURLConnection) connection; 
       httpConnection.setRequestMethod("GET"); 
       httpConnection.connect(); 

       if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { 
        stream = httpConnection.getInputStream(); 
       } 
      } catch (Exception ex) { 
       ex.printStackTrace(); 
      } 
      return stream; 
     } 
    } 

} 
+0

試試這個:http://mygmn.com/joblink/wp-admin/admin-ajax.php?action=joblink_searchcompanies&company_id=1180。排除:「www.mygmn.com」中的「www。」 – WannaBeGeek 2015-04-06 10:28:43

+0

user3467204我不知道你的意思..服務器上的數據是一樣的。我檢查了我保存在logcat變量中的數據。 – Beginner 2015-04-06 10:31:25

+0

您的服務器正在響應正確形成的圖像的網址。但無法理解你如何在imageview中加載圖像 – 2015-04-06 10:32:41

回答

0

您正在運行裏面的onCreate 2個asynctasks()方法。由於這些都是異步的GetXMLTask與執行字符串CoverImage爲空。 所以,這個代碼移動:

GetXMLTask task = new GetXMLTask(); 
task.execute(Coverimage); 

onPostExecute()方法您的詳細信息的AsyncTask將解決這個問題。

1

java.net.MalformedURLException可能是由於安全原因。您必須添加http://或https://與您的url圖像。

+0

Poras Dhiman我的網址已經包含http – Beginner 2015-04-06 10:55:29

+0

我的問題仍然保持不變。任何人都可以解決它....? – Beginner 2015-04-06 11:13:21

0

OK,使用此代碼

public class ImageLoading { 

public enum BitmapManager { 
    INSTANCE; 

    private final Map<String, SoftReference<Bitmap>> cache; 
    private final ExecutorService pool; 
    private Map<ImageView, String> imageViews = Collections 
      .synchronizedMap(new WeakHashMap<ImageView, String>()); 
    private Bitmap placeholder; 

    BitmapManager() { 
     cache = new HashMap<String, SoftReference<Bitmap>>(); 
     pool = Executors.newFixedThreadPool(5); 
    } 

    public void setPlaceholder(Bitmap bmp) { 
     placeholder = bmp; 
    } 


    public Bitmap getBitmapFromCache(String url) { 
     if (cache.containsKey(url)) { 
      return cache.get(url).get(); 
     } 
     return null; 
    } 

    public void queueJob(final String url, final ImageView imageView, 
      final int width, final int height) { 
     /* Create handler in UI thread. */ 
     final Handler handler = new Handler() { 
      @Override 
      public void handleMessage(Message msg) { 
       String tag = imageViews.get(imageView); 
       if (tag != null && tag.equals(url)) { 
        if (msg.obj != null) { 
         imageView.setImageBitmap((Bitmap) msg.obj); 
        } else { 
         imageView.setImageBitmap(placeholder); 
         Log.d(null, "fail " + url); 
        } 
       } 
      } 
     }; 

     pool.submit(new Runnable() { 
      @Override 
      public void run() { 
       final Bitmap bmp = downloadBitmap(url, width, height); 
       Message message = Message.obtain(); 
       message.obj = bmp; 
       Log.d(null, "Item downloaded: " + url); 

       handler.sendMessage(message); 
      } 
     }); 
    } 

    public void loadBitmap(final String url, final ImageView imageView, 
      final int width, final int height) { 
     imageViews.put(imageView, url); 
     Bitmap bitmap = getBitmapFromCache(url); 

     // check in UI thread, so no concurrency issues 
     if (bitmap != null) { 
      Log.d(null, "Item loaded from cache: " + url); 
      imageView.setImageBitmap(bitmap); 
     } else { 
      imageView.setImageBitmap(placeholder); 
      queueJob(url, imageView, width, height); 
     } 
    } 

    private Bitmap downloadBitmap(String url, int width, int height) { 
     try { 
      Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL( 
        url).getContent()); 
      bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true); 
      cache.put(url, new SoftReference<Bitmap>(bitmap)); 
      return bitmap; 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return null; 
    } 
} 

直接使用此代碼,並在您使用下載代碼,使用此代碼
ImageLoading.BitmapManager.INSTANCE.loadBitmap( 「HTTP://」 +圖像,holder.img,150,180);

+0

什麼是holder.img – Beginner 2015-04-06 11:20:34

+0

這是ImageView對象。 – 2015-04-06 11:24:41

+0

我試過了,但仍然不能正常工作 – Beginner 2015-04-06 11:30:12

0

您的URL可以從您的服務器響應中獲得。相反,加載圖像的嘗試手動Picasso Library

與此庫,你只需要做 -

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);