When I try to get a JSONArray
from the internet, it has an error.
Error
Error in http connection org.json.JSONException: End of input at character 0 of
This error happens when he tries to read the response from the internet. It interrupts the doInBackground and goes straight to onPostExecute ...
JSON that the 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) {
}
}