Error consuming JSON: "json Parsing error value <!doctype of type java.lang.string cannot be converted to jsonobject"

Asked

Viewed 833 times

1

I’m trying to consume a JSON in my application. I created a Splashscreen and in it I am trying to download the data from the file JSON and send to my Masteractivity, where, then, I’m going to build a Listview with this data. I am getting the following error message:

json Parsing error value <!doctype of type java.lang.string cannot be converted to jsonobject

Searching, everyone says it occurs when the address is wrong, but my address is correct, so I don’t know what might be causing it.

This is the code of mine 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();
    }*/
}
}

And this is the Handler I implemented to make the connection and convert the Stream for 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();
}
}
  • That url https://www.dropbox.com/s/d24im9i7e3tczls/carros.json?dl=0 returns an html page. For this to work it has to return only and exclusively a JSON.

  • Your application is considering an html page as if it were JSON. Since it is not, it fails to get the information you want and throws an exception.

  • I tried writing the link only until . json, but still, it wasn’t, which I can do?

  • You can download this file and put it on an Apache server, as in XAMPP, for example. I don’t know if you know...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.