我从服务器得到html响应,但图像标签不起作用

时间:2015-10-09 10:10:47

标签: android android-imageview

嗨我正在开发一个Android应用程序,我正在显示评论相关该票。但在服务器端数据库所有评论都以html格式存储。当我发送请求它我得到所有HTML代码。但我设法使用

以适当的格式转换HTML代码
  comm.setText(Html.fromHtml(cmnt[position]));

但问题是,如果评论中有图像标签,那么它将显示小框而不是图像。我想要显示该图像。 请帮帮我

Single_Ticket.java

 private class showcomment extends AsyncTask<String,String,String>
{
    ProgressDialog pdialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        Log.v("TAG", "In onPreExecute of the loading comnt.");



    }
    protected String doInBackground(String... args) {

            Log.v("TAG", "In doInBackground of the loading comment. ");

            List<NameValuePair> param = new ArrayList<NameValuePair>();
            param.add(new BasicNameValuePair("tktid", id));
            ServiceHandler sh = new ServiceHandler();


            String jsonStr = sh.makeServiceCall(URL_COMMENT, ServiceHandler.POST, param);
            Log.d("Response: ", "> " + jsonStr);
            if (jsonStr != null)

            {
                try {

                    contacts = new JSONArray(jsonStr);
                    int a=contacts.length();
                    Log.v(TAG, ".................." + a);
                    if(a > 0 ) {
                       cmnt = new String[contacts.length()];
                        cmnt_time = new String[contacts.length()];
                        cmnt_by = new String[contacts.length()];
                        for (int i = (a-1),j=0; i >= 0&& j < contacts.length(); i--,j++) {

                            JSONObject c = contacts.getJSONObject(j);
                            Log.v(TAG, "" + i);
                            String aa = c.getString("cmnt");
                            String bb = c.getString("cmnt_time");
                            String cc = c.getString("cmnt_by");

                            Log.v(TAG,""+aa);
                            cmnt[i] = aa;
                            cmnt_time[i]=bb;
                            cmnt_by[i]=cc;
                            Log.v(TAG,""+cmnt[i]);
                        }


                        }
                } catch (JSONException e) {
                    System.out.print("hiiiiiiiiiiii" );
                    e.printStackTrace();
                }
            }

            return null;
        }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        if(cmnt!=null && cmnt.length > 0 ) {
           C_adapter adapter = new C_adapter(Single_Ticket.this, cmnt,cmnt_by,cmnt_time);
            cmntview.setAdapter(adapter);
        }

    }
}

C_adapter.java

public class C_adapter extends ArrayAdapter   {

Context context;
String[] cmnt;
String[] cmnt_by;
String[] cmnt_time;
LayoutInflater inflater;


public C_adapter(Single_Ticket context, String[] cmnt,String[] cmnt_by,String[] cmnt_time) {
    super(context, R.id.cmnt_list,cmnt);
    this.context=context;
    this.cmnt=cmnt;
    this.cmnt_by=cmnt_by;
    this.cmnt_time=cmnt_time;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.cmnt_list, null);

    }
    TextView comm = (TextView) convertView.findViewById(R.id.cmnt);
    TextView cmntby = (TextView) convertView.findViewById(R.id.cmntby);

    comm.setText(Html.fromHtml(cmnt[position]));
    if(cmnt_by[position].equalsIgnoreCase("Lalit Patil")|| cmnt_by[position].equalsIgnoreCase("Sarvesh Sonawane"))
    {
        //cmntby.setBackgroundResource(R.color.orange_red);
        cmntby.setText(cmnt_time[position]+" - "+cmnt_by[position]);

    }
    cmntby.setText(cmnt_time[position]+" - "+cmnt_by[position]);

    return convertView;
}



   }

我收到了像这样的服务器的响应

{"cmnt":"<img src=\"http:\/\/i.imgur.com\/JHcdBVj.jpg\" width=\"448\"><br>","cmnt_time":"Wed,07 Oct 2015 05:41pm","cmnt_by":"test test"}

1 个答案:

答案 0 :(得分:0)

ImageView TextView 放在 layout.xml中的相同位置 档案。

然后检查从服务器获取的数据内容,如果数据包含 img 标记,则 设置为对图片视图可见并设置位图到图像视图 否则 设置为文本视图

我为你做了一个功能。我使用Picasso在图片视图中设置图片,使用Picasso 我们可以在图片视图中设置只有1行的图片。

public void setImage() {
        try {
            String response = "{\"cmnt\":\"<img src=\\\"http:\\/\\/i.imgur.com\\/JHcdBVj.jpg\\\" width=\\\"448\\\"><br>\",\"cmnt_time\":\"Wed,07 Oct 2015 05:41pm\",\"cmnt_by\":\"test test\"}";

            JSONObject jsonresponse = new JSONObject(response);
            String cmnt = jsonresponse.getString("cmnt");
            if (cmnt.contains("<img")) {
                textView.setVisibility(View.GONE);
                imageView.setVisibility(View.VISIBLE);
                Pattern p = Pattern.compile("<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>");
                Matcher m = p.matcher(cmnt);
                if (m.find()) {
                    cmnt = m.group(1);
                }
                Picasso.with(this)
                        .load(cmnt)
                        .into(imageView);
            } else {
                textView.setVisibility(View.VISIBLE);
                imageView.setVisibility(View.GONE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }