I've been trying to parse data from here: https://jchui.xyz/athena/data/getNewsContent.php?id=23171 into a RecyclerView.
I'm not super experienced with programming but I've managed to parse JSON data in the past and I'm not exactly sure what is wrong this time. Below is the relevant code.
I'm getting an error:
W/System.err: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
Any help would be much appreciated!
Details Activity:
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();
}
}
}
}