0
When I try to catch one JSONArray
of the internet, it presents an error.
Error
Error in http connection org.json.JSONException: End of input at character 0 of
This error happens when it will try to read the response from the internet. It interrupts doInBackground and goes straight to onPostExecute...
JSON that link returns:
[{"id":"2","data":"03\/06\/2016 11:11:20","texto":"Mande suas mensagens","nome":null},{"id":"1","data":"02\/06\/2016 11:28:10","texto":"Bom dia!","nome":null}]
Code:
public void carregaMural(View v){ new LongOperation2().execute("");}
private class LongOperation2 extends AsyncTask<String, Void, String> {
ProgressBar carrega = (ProgressBar) findViewById(R.id.carregaMsg);
public char getChar(char x){return x;}
@Override
protected String doInBackground(String... params) {
try {
verifica = '0'; //SE FOR INTERROMPIDO, TENTA NOVAMENTE. . .
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.siteteste.com.br/app/listener.php?a=10");
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-type", "application/json; charset=utf-8");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
resultRecebe = sb.toString();
is.close();
jsonArray = new JSONArray(result);
Log.v("JSON", jsonArray.toString());
verifica = '1';
} catch (SocketException e) {
Log.e("log_tag", "Error in SocketException "
+ e.toString());
} catch (Exception e) {
Log.e("log_tag", "Error in http connection "
+ e.toString());
}
return null;
}
@Override
protected void onPostExecute(String result) {
carrega.setVisibility(View.INVISIBLE);
if(verifica == '0'){//FOI INTERROMPIDO?
new LongOperation2().execute(""); //TENTA NOVAMENTE
}else{
mandaLista();
}
}
@Override
protected void onPreExecute() {
carrega.setVisibility(View.VISIBLE);
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
In your doInbackground instead of putting Return null, put Return checks. And put checks = '0', before Try{}.
– Bruno Romualdo
Try using GET instead of your POST, you’re going to fetch URL
– FábioArsénio
Does the error not occur when trying to transform into JSON ? the error occurs in org.json.Jsonexception !!! Could you put the whole error? Then fail in the solution! Thank you!
– Thiago Luiz Domacoski
True, Listener.php is returning with json_encode()?
– Bruno Romualdo
Putting the GET in place of the POST it stopped giving the error, then it started giving null Pointer Exception but, this was due to a same syntax error. Jsonarray was receiving the variable result and was to receive the resultReceie. Now it’s working well. Thank you :))
– CristianCotrena