图像不加载,throwingjava.net.MalformedURLException [英] Image does not loaded and throwingjava.net.MalformedURLException

查看:180
本文介绍了图像不加载,throwingjava.net.MalformedURLException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我解析一个JSON字符串从中我得到的图像的URL。然后我传递网址的方法来获取图像,而在ImageView的显示,但图像没有加载并抛出java.net.MalformedURLException的异常。当我尝试直接传递图像URL的方法,那么它就会被加载。所以我不知道哪里是错误。任何帮助将AP preciated。提前致谢。我的code是低于

 公共类CompanyDetailActivity延伸活动{    ImageView的coverimage;
    ImageView的profileimage;
    TextView的全名;
    TextView的标语;
    TextView的行业;
    TextView的teamsize;
    TextView的约;
    TextView的一年;
    TextView的位置;    串Coverimage;
    串Profimage;
    字符串全名;
    串标语;
    字符串行业;
    串Teamsize;
    关于字符串;
    字串年;
    字符串位置;    //位图位图;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.companydetails);        coverimage =(ImageView的)findViewById(R.id.C​​overImage);
        profileimage =(ImageView的)findViewById(R.id.ProfileImage);
        全名=(的TextView)findViewById(R.id.FullName);
        标语=(的TextView)findViewById(R.id.TagLine);
        业内=(的TextView)findViewById(R.id.IndustryName);
        teamsize =(的TextView)findViewById(R.id.TeamSize);
        关于=(的TextView)findViewById(R.id.C​​ompanyAbout);
        年=(的TextView)findViewById(R.id.FoundYear);
        位置=(的TextView)findViewById(R.id.Location);        新DetailsAsynTask()
                .execute(\"http://www.mygmn.com/joblink/wp-admin/admin-ajax.php?action=joblink_searchcompanies&company_id=1180\");        GetXMLTask任务=新GetXMLTask();
        task.execute(Coverimage);    }    公共类DetailsAsynTask扩展的AsyncTask<弦乐,太虚,布尔> {        @覆盖
        保护布尔doInBackground(字符串...为arg0){
            尝试{                HttpClient的客户端=新DefaultHttpClient();
                HttpPost后=新HttpPost(arg0中[0]);                HTT presponse响应= client.execute(岗位);                。INT状态= response.getStatusLine()的getStatus code();
                如果(状态== 200){
                    //得到响应
                    HttpEntity实体= response.getEntity();
                    字符串数据= EntityUtils.toString(实体);                    JSONObject的jObj =新的JSONObject(数据);
                    JSONObject的MainObject = jObj.getJSONObject(数据);
                    CompanyDetailsModel公司=新CompanyDetailsModel();
                    Coverimage = company.setCoverImage(MainObject
                            .getString(cove_img));                    Profimage = company.setCompanyProfPicture(MainObject
                            .getString(company_profile_picture));                    全名= company.setCompanyFullName(MainObject
                            .getString(company_full_name));                    标语= company.setComapnyTagLine(MainObject
                            .getString(company_tagline));
                    业内= company.setCompanyInustry(MainObject
                            .getString(company_industry));
                    Teamsize = company.setCompanyTeamSize(MainObject
                            .getString(company_teamsize));
                    关于= company.setCompanyAbout(MainObject
                            .getString(company_about));
                    年= company.setCompanyFoundYear(MainObject
                            .getString(company_foundyear));
                    位置= company.setCompanyLocation(MainObject
                            .getString(公司所在地));                    返回true;
                }
            }赶上(ClientProtocolException E){
                e.printStackTrace();
            }赶上(IOException异常五){
                e.printStackTrace();
            }赶上(JSONException E){
                // TODO自动生成catch块
                e.printStackTrace();
            }
            返回false;
        }        @覆盖
        保护无效onPostExecute(布尔结果){
            super.onPostExecute(结果);
            如果(结果== FALSE){            }其他{                fullname.setText(全名);
                tagline.setText(标语);
                industry.setText(工业);
                teamsize.setText(Teamsize);
                about.setText(关于);
                year.setText(年);
                location.setText(位置);            }
        }    }    私有类GetXMLTask扩展的AsyncTask<弦乐,太虚,位图> {
        @覆盖
        保护位图doInBackground(字符串的URL ...){
            位图贴图= NULL;
            对于(字符串网址:网址){
                地图= downloadImage(URL);
            }            返回地图;
        }        //设置由doInBackground返回的位图
        @覆盖
        保护无效onPostExecute(位图结果){
            coverimage.setImageBitmap(结果);
        }        //从InputStream创建位图并返回
        私人位图downloadImage(字符串URL){
            位图位图= NULL;
            InputStream的流= NULL;
            BitmapFactory.Options bmOptions =新BitmapFactory.Options();
            bmOptions.inSampleSize = 1;            尝试{
                流= getHttpConnection(URL);
                位= BitmapFactory.de codeStream(流空,bmOptions);
                stream.close();
            }赶上(IOException异常E1){
                e1.printStackTrace();
            }
            返回位图;
        }        //使HttpURLConnection类,返回的InputStream
        私人的InputStream getHttpConnection(字符串urlString)
                抛出IOException
            InputStream的流= NULL;
            网址URL =新的URL(urlString);
            URLConnection的连接= url.openConnection();            尝试{
                HttpURLConnection类的HttpConnection =(HttpURLConnection类)连接;
                httpConnection.setRequestMethod(GET);
                httpConnection.connect();                如果(httpConnection.getResponse code()== HttpURLConnection.HTTP_OK){
                    流= httpConnection.getInputStream();
                }
            }赶上(例外前){
                ex.printStackTrace();
            }
            返回流;
        }
    }}


解决方案

您正在运行里面的onCreate 2 asynctasks()方法。由于这些都是异步的GetXMLTask是为空字符串与执行CoverImage。
所以,这个移动code:

  GetXMLTask任务=新GetXMLTask();
task.execute(Coverimage);

onPostExecute()您的详细方法的AsyncTask会解决这个问题。

i am parsing a json string from which i get the url of image. then i pass that url to a method to get the image and display it in an imageview but the image does not loaded and throws an exception of java.net.MalformedURLException. when i try to pass the image url directly to the method then it gets loaded. so i dont know where is the error. Any help will be appreciated. thanks in advance. my code is below

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;
        }
    }

}

解决方案

You are running two asynctasks inside onCreate() method .As these are asynchronous your GetXMLTask was executed with String CoverImage as null . So , moving this code :

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

to the onPostExecute() Method of your Details asynctask will solve the problem .

这篇关于图像不加载,throwingjava.net.MalformedURLException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆