如何从JSON网址获取数据时删除html标签

时间:2018-08-08 05:31:51

标签: android json android-studio textview

这是我的代码,它从url中获取json,并且json包含诸如(<p>,</p>,<br>)之类的html标签,同时获取标签也显示在textview中的数据,所以我的问题是如何在不使用行分隔符的情况下删除这些标签或替换方法。

我已在显示时上传了包含html标签的输出图片,我不希望这些图片显示。enter image description here

public class MainActivity extends AppCompatActivity  {

    private String TAG = MainActivity.class.getSimpleName();

    private ProgressDialog pDialog;
    private ListView lv;

    private Button w_sharee;

    private static String url = "http://mantras.happylife.in/mantras_api";

    ArrayList<HashMap<String, String>> contactList;

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

        contactList = new ArrayList<>();

        lv = (ListView) findViewById(R.id.list);

        new GetContacts().execute();

    }

    class ItemList implements AdapterView.OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ViewGroup vg = (ViewGroup)view;
            TextView ti = (TextView)vg.findViewById(R.id.title);
            TextView de = (TextView)vg.findViewById(R.id.desc);

            String selected = ti.getText().toString() + de.getText().toString();
            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setType("text/plain");
            intent.putExtra(Intent.EXTRA_TEXT,selected);
            startActivity(Intent.createChooser(intent,"Share via"));

//            Toast.makeText(MainActivity.this, ti.getText().toString() + de.getText().toString(), Toast.LENGTH_SHORT).show();


        }
    }


    private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Please wait...");
            pDialog.setCancelable(false);
            pDialog.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            HttpHandler sh = new HttpHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url);

            Log.e(TAG, "Response from url: " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONArray hotboxs = new JSONArray(jsonStr);

                    for (int i = 0; i < hotboxs.length(); i++) {
                        JSONObject c = hotboxs.getJSONObject(i);


//                        if (c.get("configurationId").equals(7)) {



//                        String lineSep = System.getProperty("line.separator");

                        String title = c.getString("title");
                        String desc = c.getString("desc");
//
//                        desc= desc.replace("<br />", lineSep);
//                        desc= desc.replace("<p>", lineSep);
//                        desc= desc.replace("</p>", lineSep);
//                        desc= desc.replace("<li>", lineSep);
//                        desc= desc.replace("</li>", lineSep);



                        HashMap<String, String> contact = new HashMap<>();
                            contact.put("title", title);
                            contact.put("desc", desc);
                            contactList.add(contact);
//                        }

                    }
                } catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Json parsing error: " + e.getMessage(),
                                    Toast.LENGTH_LONG)
                                    .show();
                        }
                    });

                }
            } else {
                Log.e(TAG, "Couldn't get json from server.");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Couldn't get json from server. Check LogCat for possible errors!",
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * */
            ListAdapter adapter = new SimpleAdapter(
                   MainActivity.this, contactList,
                  R.layout.list_item, new String[]{"title","desc"}, new int[]{R.id.title, R.id.desc});
          lv.setAdapter(adapter);
          lv.setOnItemClickListener(new ItemList());


        }

    }
}

0 个答案:

没有答案