我希望描述显示,但获取整个json数据

时间:2017-11-18 17:22:39

标签: android json webview

我无法在我的JSON中获取特定字符串描述中的数据。而不是我在点击listitem时在我的webview中获取整个JSON数据。在webview中我得到了json文件中显示的整个json,我只希望在webview中显示描述。帮助

json文件

{
"status": "ok",
"source": "techcrunch",
"sortBy": "top",
-"articles": [
-{
"author": "Devin Coldewey",
"title": "A mesh network spontaneously erupts in the US and helps connect Puerto Rico",
"description": "When goTenna put out their Mesh device earlier this year, I thought the off-grid gadgets would be great for an emergency kit or back-country hike. But it..",
"url": "https://techcrunch.com/2017/11/14/a-mesh-network-spontaneously-erupts-in-the-us-and-helps-connect-puerto-rico/",
"urlToImage": "https://tctechcrunch2011.files.wordpress.com/2017/11/gotennapr.png",
"publishedAt": "2017-11-14T18:41:28Z"

}

    package com.example.murarilal.volley;

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

    public class ArticleActivity extends AppCompatActivity {


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_article);
            String url="https://newsapi.org/v1/articles?source=techcrunch&apiKey=59b308aec9f242fe98b527ab9ba93199";


            WebView webView=(WebView)findViewById(R.id.webView);
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setWebViewClient(new WebViewClient());

            Intent intent=getIntent();
            webView.loadDataWithBaseURL(url,null,intent.getStringExtra("description"),"text/html","UTF-8");
            webView.loadUrl(url);



        }

    }
package com.example.murarilal.volley;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    RequestQueue queue;
    ListView listView;
    List<Hero> heroList;

    private String url="https://newsapi.org/v2/top-headlines?sources=techcrunch&apiKey=d6eaad863cc640a7a6864d18196bea9e";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        queue= Volley.newRequestQueue(this);
        home();

        listView = (ListView) findViewById(R.id.listView);
        heroList = new ArrayList<>();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Hero hero = heroList.get(i);
                Intent intent=new Intent(getApplicationContext(),ArticleActivity.class);
                //hero want to use any method from your hero object, example : getImageURL();
                intent.putExtra("description",hero.getImage());
                startActivity(intent);

            }
        });

    }

    public void home()
    {
        JsonObjectRequest request =new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                try {
                    JSONObject obj = new JSONObject(String.valueOf(response));

                    JSONArray array = obj.getJSONArray("articles");

                    for (int i = 0; i < array.length(); i++) {
                        Hero hero=new Hero( (String) array.getJSONObject(i).get("title"),(String) array.getJSONObject(i).get("urlToImage"));
                        heroList.add(hero);
                    }

                    ListViewAdapter adapter = new ListViewAdapter(heroList, getApplicationContext());
                    listView.setAdapter(adapter);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
                , new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(request);
    }
}

package com.example.murarilal.volley;  这是英雄级

public class Hero {

    String description,image;

    public Hero(String description,String image) {
        this.description = description;
        this.image=image;
    }


    public String getDescription()
    {
        return description;
    }
    public String getImage()

    {
        return image;
    }


}

2 个答案:

答案 0 :(得分:0)

    text = text.replace('\n', ' ');
    text = text.replace("\n", "");
    text = text.replace("\\n", "");
    text = text.replaceAll("\\n", "");
    text = text.replaceAll("\\\\n", "");

<强>更新

我给你的代码没问题,我正在测试你的网址并且上面有错误:

tv.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY, null, new CustomTagHandler(context, cp)));

答案 1 :(得分:0)

devesh。如果您只想在webview中显示文章,那么首先点击URL并获取ArrayList中的所有文章或直接获取JSONArray。 1.如果您使用了arraylist,则使用GSON库OR将该数组列表转换为字符串 2.如果您直接获得JSONArray文章,而不是使用toString()将jsonArray转换为字符串。 之后,创建一个HTML字符串,并将此文章字符串附加到此HTML字符串的正文部分中,如: