如何在asynctask onPostExecute上从服务器上显示imageview上的图像?

时间:2015-11-23 02:30:38

标签: android mysql bitmap android-asynctask

我的mysql中有一个文章表,每篇文章都有一个图像。

**article_id**    **article_image**
00001         display/Article1image.png
00002         display/Article2image.jpg
00003         display/Article3image.jpg
00004         display/Article4image.png

假设我正在使用不同的视图(如article_content,art​​icle_writer,date_published等)在linearlayout上打印所有文章信息,并且我想在以编程方式创建的ImageView上显示article_image

假设`“www.sampleweb.com /”+ _ jsonObject.optString(“article_image”)。toString()'是来自服务器的图像的完整链接。

以下是我的完整代码,如何使用上述网址的内容创建图片视图?

@Override
    public void onPostExecute(String result) {
        //headlinehead.setText(result);
        try{
            JSONObject jsonRootObj = new JSONObject(result);
            JSONArray jsonArray = jsonRootObj.optJSONArray("Data");
            JSONObject jsonObject = jsonArray.getJSONObject(0);
            //code for headline
            this.headlinehead.setText(jsonObject.optString("title").toString());
            this.shortdesc.setText(jsonObject.optString("shortdesc").toString());
            this.headlinesender.setText(jsonObject.optString("penname").toString());
            this.img.setTag(jsonObject.optString("id").toString());
            //code for headline
            for (int p = 1; p < jsonArray.length(); p++){
                JSONObject _jsonObject = jsonArray.getJSONObject(p);
                LinearLayout conteach = new LinearLayout(getActivity().getApplicationContext());
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                conteach.setLayoutParams(lp);
                conteach.setOrientation(LinearLayout.VERTICAL);

                TextView art_title = new TextView(getActivity().getApplicationContext());
                art_title.setText(_jsonObject.optString("title").toString());
                art_title.setTextSize(20);
                art_title.setLayoutParams(new LayoutParams(350,LayoutParams.WRAP_CONTENT));

                TextView art_shortdesc = new TextView(getActivity().getApplicationContext());
                art_shortdesc.setText(_jsonObject.optString("shortdesc").toString());
                art_shortdesc.setTextSize(14);
                art_shortdesc.setTypeface(null, Typeface.ITALIC);
                art_shortdesc.setLayoutParams(new LayoutParams(350,LayoutParams.WRAP_CONTENT));

                TextView art_sender = new TextView(getActivity().getApplicationContext());
                art_sender.setText("Written by: " + _jsonObject.optString("sender").toString());
                art_sender.setTextSize(14);
                art_sender.setLayoutParams(new LayoutParams(350,LayoutParams.WRAP_CONTENT));
                art_sender.setPadding(0, 0, 0, 15);


                conteach.addView(art_title);
                conteach.addView(art_shortdesc);
                conteach.addView(art_sender);
                conteach.setTag(_jsonObject.optString("id").toString());
                conteach.setClickable(true);
                conteach.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {
                        SpecificArticleFragment spcf = new SpecificArticleFragment();
                        Bundle args = new Bundle();
                        args.putString("art_id", v.getTag().toString());
                        spcf.setArguments(args);

                        FragmentManager ft = getFragmentManager();
                        ft.beginTransaction().replace(R.id.content_frame, spcf).commit();
                    }
                 });                
                this.llist.addView(conteach);

            }
            this.pbColBul.setVisibility(ProgressBar.GONE);
        } catch(JSONException e){

        }
    }

1 个答案:

答案 0 :(得分:0)

以编程方式创建ImageView,就像使用Textview一样。使用Picasso将网址加载到其中。它做得很好 并提供一些有用的方法,当图像装入开箱即用时,淡入效果 至于JSON解析,我建议将RetrofitOkHttp客户端一起使用。它比你的实现容易得多。此外,Retrofit可以在后台进行API调用,因此请删除AsyncTask