Jsonexception error

Asked

Viewed 40 times

0

My code makes a get via book ISBN :

final Button btnBuscarCEP = (Button) findViewById(R.id.btnChamaBuscaCEP);
    final EditText editTextCep = (EditText) findViewById(R.id.edtCep);

    btnBuscarCEP.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(!TemConexao()){

                CEP cep = new CEP();
                cepEntrada = editTextCep.getText().toString();
                editTextCep.setText("");
                urlapi = "http://www.isbnsearch.org/isbn/" + cepEntrada;
                if(cepEntrada.length() == 10){
                    progressDialog =ProgressDialog.show(BuscaCep.this, "Caregando . . . ","", true);
                    progressDialog.setCancelable(true);
                    new ProcessJSON().execute(urlapi);
                }else{
                    Toast.makeText(getApplicationContext(), "Por favor informe um CEP válido.", Toast.LENGTH_LONG).show();
                }

            }else{
                Toast.makeText(getApplicationContext(), "Por favor verifique a conectividade de seu dispositivo.", Toast.LENGTH_LONG).show();
            }
        }
    });
}

private boolean TemConexao() {
    boolean lblnRet = false;
    try
    {
        ConnectivityManager cm = (ConnectivityManager)
                getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected()) {
            lblnRet = true;
        } else {
            lblnRet = false;
        }
    }catch (Exception e) {

    }
    return lblnRet;
}

public void toast (String msg){
    Toast.makeText (getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
}

private class ProcessJSON extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... strings) {
        String stream = null;
        String urlString = strings[0];

        HTTPDataHandler hh = new HTTPDataHandler();
        stream = hh.GetHTTPData(urlString);

        return stream;
    }

        protected void onPostExecute(String stream){

            // Toast.makeText(getApplicationContext(), "stream -" + stream, Toast.LENGTH_SHORT).show();

            if(stream !=null){
                try{
                    // Get the full HTTP Data as JSONObject

                    JSONObject reader= new JSONObject(stream);

                    String cep = reader.getString("Author");
                    String logradouro = reader.getString("Binding");
                    String complemento = reader.getString("Publisher");
                    String bairro = reader.getString("Published ");
                  //  String localidade = reader.getString("localidade");
                  //  String uf = reader.getString("pageCount");
                   // String ibge = reader.getString("description");

                    DB db = new DB(BuscaCep.this);
                    CEP c = new CEP();
                   // c.setCep(cep);
                    c.setLogradouro(logradouro);
                    c.setComplemento(complemento);
                    c.setBairro(bairro);
              //      c.setLocalidade(localidade);
               //     c.setUf(uf);
             //       c.setIbge(ibge);
                    db.salvar(c);


                    System.out.println(stream);


                    progressDialog.dismiss();

                    final TextView txtCep = (TextView) findViewById(R.id.txtBC_cep);
                    final TextView txtLogradouro = (TextView) findViewById(R.id.txtBC_logradouro);
                    final TextView txtComplemento = (TextView) findViewById(R.id.txtBC_complemento);
                    final TextView txtBairro = (TextView) findViewById(R.id.txtBC_bairro);
                    final TextView txtLocalidade = (TextView) findViewById(R.id.txtBC_localidade);
                    final TextView txtIbge = (TextView) findViewById(R.id.txtBC_ibge);


                    txtCep.setText("CEP "+cep);
                    if(logradouro != " "){
                        txtLogradouro.setText("LOGRA. "+logradouro);
                    }else{
                        txtLogradouro.setText("SEM LOGRADOURO");
                    }
                    if(complemento != " "){
                        txtComplemento.setText("COMPL. "+complemento);
                    }else{
                        txtComplemento.setText("SEM COMPLEMENTO");
                    }
                    if(bairro != " "){
                        txtBairro.setText("BAIRRO  "+bairro);
                    }else{
                        txtBairro.setText("SEM BAIRRO");
                    }
             //       txtLocalidade.setText(localidade + " / " + uf);
           //         txtIbge.setText("IBGE "+ibge);

                }catch(JSONException e){
                    e.printStackTrace();
                }
            }
        }
    }
}

My mistake is in the lines :

 5-28 16:37:36.058 2449-2449/com.XXXXXX.ceprest W/System.err: 
 org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be     
 converted to JSONObject

 05-28 16:37:36.059 2449-2449/com.XXXXXXX.ceprest W/System.err:     at 
  org.json.JSON.typeMismatch(JSON.java:111)
 05-28 16:37:36.059 2449-2449/com.XXXXXXXXX.ceprest W/System.err:     at 
 org.json.JSONObject.<init>(JSONObject.java:160)
 05-28 16:37:36.059 2449-2449/com.XXXXXXX.ceprest W/System.err:     at    
 org.json.JSONObject.<init>(JSONObject.java:173)
 05-28 16:37:36.059 2449-2449/com.XXXXXXXX.ceprest W/System.err:     at 
 com.XXXXXXX.ceprest.BuscaCep$ProcessJSON.onPostExecute(BuscaCep.java:109) 
 05-28 16:37:36.059 2449-2449/com.mulataporno.ceprest W/System.err:     at 
 com.XXXXXXX.ceprest.BuscaCep$ProcessJSON.onPostExecute(BuscaCep.java:88)

1 answer

1

Thiago,

You can pass a valid URL for testing?

I put a zip code but nothing came..

makes it easier to help.

abs

  • http://www.isbnsearch.org/isbn/9788501075680 the name of the variable is as cep,but I used a test code, it is from ISBN of books...

  • Thiago, does not return a JSON, but HTML.

  • Use another library to convert https://jsoup.org/

Browser other questions tagged

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