Estou tentando consumir um JSON em minha aplicação. Eu criei uma SplashScreen e, nela, eu estou tentando baixar os dados do arquivo JSON e mandar para minha MasterActivity, onde, depois, eu vou construir uma ListView com estes dados. Estou recebendo a seguinte mensagem de erro:
json parsing error value <!doctype of type java.lang.string cannot be converted to jsonobject
Pesquisando, todos dizem que isso ocorre quando o endereço está errado, mas meu endereço está correto, então, não sei o que pode estar ocasionando isso.
Este é o código da minha SplashScreen:
public class SplashScreen extends AppCompatActivity {
//private final int DELAY = 3000; //3 seconds
private final String URL = "https://www.dropbox.com/s/d24im9i7e3tczls/carros.json?dl=0";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_splash_screen);
/*Handler h = new Handler();
h.postDelayed(this, DELAY);*/
new GetCarros().execute();
}
/*
@Override
public void run() {
startActivity(new Intent(this, MasterActivity.class));
finish();
}*/
private class GetCarros extends AsyncTask<Void, Void, Void> {
Bundle params;
@Override
protected Void doInBackground(Void... voids) {
HttpHandler sh = new HttpHandler();
//faz a request e recebe a resposta
String jsonStr = sh.makeServiceCall(URL);
if(jsonStr != null) {
try{
JSONObject jsonObj = new JSONObject(jsonStr);
JSONArray carros = jsonObj.getJSONArray("carros");
//percorrendo array e pegando times
for(int i = 0; i < carros.length(); i++){
JSONObject c = carros.getJSONObject(i);
String id = c.getString("id");
String foto = c.getString("foto");
String modelo = c.getString("modelo");
String fabricante = c.getString("fabricante");
String ano = c.getString("ano");
String cor = c.getString("cor");
String preco = c.getString("preco");
params.putString("id", id);
params.putString("foto", foto);
params.putString("modelo", modelo);
params.putString("fabricante", fabricante);
params.putString("ano", ano);
params.putString("cor", cor);
params.putString("preco", preco);
Intent intent = new Intent(SplashScreen.this, MasterActivity.class);
intent.putExtras(params);
startActivity(intent);
finish();
}
}catch (final JSONException e) {
e.printStackTrace();
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "JSON Parsing Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}else{
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Couldn't get JSON from server.", Toast.LENGTH_LONG).show();
}
});
}
return null;
}
/*
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Intent intent = new Intent(SplashScreen.this, MasterActivity.class);
intent.putExtras(params);
startActivity(intent);
finish();
}*/
}
}
E este é o Handler que implementei para fazer a conexão e converter o Stream para String:
public class HttpHandler {
public String makeServiceCall(String reqUrl) {
String response = null;
try{
//faz o request
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
//pega a resposta
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
}catch (Exception e ){
e.printStackTrace();
}
return response;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try{
while((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
}catch (Exception e) {
e.printStackTrace();
} finally {
try{
is.close();
}catch (Exception e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
https://www.dropbox.com/s/d24im9i7e3tczls/carros.json?dl=0
retorna uma página html. Pra que isso funcione tem que retornar única e exclusivamente um JSON.