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.

  • Output Log.d(LOG_TAG, strJson); before creating a JSONObject and see if json really is there. - Suvitruf
  • @Suvitruf program does not even reach the output, the error still crashes to doInBackground`e .. - ivanovd422

1 answer 1

You server responds with an error 400 in the status. In the case of a status greater than or equal to 400 (this means an error has occurred), the response from the server will not lie in urlConnection.getInputStream() , but in urlConnection.getErrorStream()

Total, you need to check:

 InputStream inputStream if(urlConnection.getResponseCode() == 200) { inputStream = urlConnection.getInputStream(); } else { inputStream = urlConnection.getErrorStream(); } 
  • Interestingly, I did not know about it, I thought that in case of an error the answer would lie there too. Be sure to try, thank you for the answer! - ivanovd422