There is a code that receives a JSON file by reference LINK
class MyTask extends AsyncTask<String, Void, String> { HttpURLConnection urlConnection = null; BufferedReader reader = null; String resultJson = ""; @Override public String doInBackground(String... urls) { try { URL url = new URL(**LINK**); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.connect(); InputStream inputStream = urlConnection.getInputStream(); StringBuffer buffer = new StringBuffer(); reader = new BufferedReader(new InputStreamReader(inputStream)); String line; while ((line = reader.readLine()) != null) { buffer.append(line); } resultJson = buffer.toString(); } catch (Exception e) { e.printStackTrace(); } return resultJson; }
Then, it sends information to the onPostExecute method, which, let's say, simply logs this information.
@Override public void onPostExecute(String strJson) { super.onPostExecute(strJson); try { //SONObject dataJsonObj = null; JSONObject dataJsonObj = new JSONObject(strJson); Log.d(LOG_TAG, strJson); Log.d(LOG_TAG, "JSON " + dataJsonObj); } catch (JSONException e) { e.printStackTrace(); } }
If LINK link is specified to a file with objects and keys / values, for example, https://api.twitch.tv/kraken/streams/igromania
{"stream": null, "_ links": {"self": " https://api.twitch.tv/kraken/streams/igromania ", "channel": " https://api.twitch.tv/kraken / channels / igromania "}}
That information is displayed in the log. And if there is a link to the file WITHOUT objects, but with keys / values, for example http://api.twitch.tv/kraken/streams/ajfafaasf
{"error": "Bad Request", "message": "Requests must be made over SSL", "status": 400}
then nothing is written to the log and several exceptions are thrown:
org.json.JSONException: End of input at character 0 of java.io.FileNotFoundException: http://api.twitch.tv/kraken/streams/ajfafaasf
from the last line I realized that the file was not found, but follow the link and check for yourself, it is there. What is the problem here? Maybe you can somehow make a check for the presence of arrays or objects (which, in turn, include keys / values) Tried to state your problem as clearly as possible, I hope you can tell you how to treat this problem.
Log.d(LOG_TAG, strJson);
before creating aJSONObject
and see if json really is there. - Suvitruf ♦