将JSON数据解析为RecyclerView

时间:2016-06-12 15:36:30

标签: java android json

我一直在尝试从这里解析数据:https://jchui.xyz/athena/data/getNewsContent.php?id=23171到RecyclerView。

我对编程没有超级经验,但我过去设法解析了JSON数据,而且我不确定这次出了什么问题。以下是相关代码。

我收到错误:

  

W / System.err:javax.net.ssl.SSLHandshakeException:java.security.cert.CertPathValidatorException:找不到证书路径的信任锚。

任何帮助将不胜感激!

详情活动:

public class Details extends AppCompatActivity {

private static final String KEY_LINK = "link";

RequestQueue requestQueue;

public static final int CONNECTION_TIMEOUT = 10000;
public static final int READ_TIMEOUT = 15000;

private RecyclerView rv;
private CardView cardView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    new GetData().execute();

}

private class GetData extends AsyncTask<String, String, String> {

    HttpsURLConnection conn;
    URL url = null;

    @Override
    protected String doInBackground(String... params) {
        try {
            url = new URL("https://jchui.xyz/athena/data/getNewsContent.php?id=23171");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        try {
            conn = (HttpsURLConnection) url.openConnection();
            conn.setReadTimeout(READ_TIMEOUT);
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setRequestMethod("GET");

            // setDoOutput to true as we recieve data from json file
            conn.setDoOutput(true);

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return e1.toString();
        }

        try {

            int response_code = conn.getResponseCode();

            // Check if successful connection made
            if (response_code == HttpURLConnection.HTTP_OK) {

                // Read data sent from server
                InputStream input = conn.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(input));
                StringBuilder result = new StringBuilder();
                String line;

                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }

                // Pass data to onPostExecute method
                return (result.toString());

            } else {

                return ("unsuccessful");
            }

        } catch (IOException e) {
            e.printStackTrace();
            return e.toString();
        } finally {
            conn.disconnect();
        }
    }

    @Override
    protected void onPostExecute(String result) {

        List<Detail> detailList = new ArrayList<>();

        try {
            JSONArray jsonArray = new JSONArray(result);

            for(int i=0;i<jsonArray.length();i++){
                JSONObject json_data = jsonArray.getJSONObject(i);
                Detail details = new Detail();
                details.title = json_data.getString("title");
                detailList.add(details);

            }

            cardView = (CardView) findViewById(R.id.cv);
            rv = (RecyclerView) findViewById(R.id.rv);

            LinearLayoutManager llm = new LinearLayoutManager(Details.this);
            rv.setLayoutManager(llm);

            RVDetailAdapter adapter = new RVDetailAdapter(detailList);
            rv.setAdapter(adapter);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
}

0 个答案:

没有答案