mcxiaoke / android-volley

DEPRECATED
4.29k stars 1.56k forks source link

JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONArray #143

Open jfernandojr opened 8 years ago

jfernandojr commented 8 years ago

I'm having trouble compiling the method below

private void loadData() throws JSONException {

    JSONObject objTeste = new JSONObject()
            .put("imei", "459710040904196")
            .put("acao", "pos");

    DevicesUserRequest request = new DevicesUserRequest
            (objTeste, new Response.Listener<JSONArray>() {

                @Override
                public void onResponse(JSONArray response) {
                    mItems.clear();
                    for (int i = 0; i < response.length(); ++i) {
                        try {
                            JSONObject data = response.getJSONObject(i);
                            Long id = data.getLong("veiculo_id");
                            String name = data.getString("tipo_veiculo");

                            VeiculosModel item = new VeiculosModel(id, name);

                            Double latitude = data.getDouble("latitude");
                            Double longitude = data.getDouble("longitude");
                            item.setmLatitude(latitude);
                            item.setmLongitude(longitude);

                            if (data.has("placa_veiculo")) {
                                String speed = data.getString("placa_veiculo");
                                item.setSpeed(speed);
                            }

                            if (data.has("placa_veiculo")) {
                                String placa = data.getString("placa_veiculo");
                                item.setmName(placa);
                            }

                            mItems.add(item);
                        } catch (JSONException e) {
                            Log.e(TAG, "Error while reading marker from response", e);
                        }
                    }

                    Collections.sort(mItems);

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e(TAG, "Olha o erro aqui!", error);
                    Toast.makeText(getApplicationContext(), "Verifique sua conexão.",
                            Toast.LENGTH_SHORT).show();
                }
            });

    getRequestQueue(getApplicationContext()).add(request);

}

It is showing the following error: com.android.volley.ParseError: org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONArray

Below is the class DevicesUserRequest:

public class DevicesUserRequest extends JsonArrayRequest {

private static final String Protocol = "http://";
private static final String Host = "192.241.251.229";
private static final String Port = "8080";
private static final String FullHost = Protocol + Host + ":" + Port;

//    private static final String sUrl = "http://gogps.com.br/gogps-rest/api/device/getDeviceUser";
private static final String sUrl = FullHost;

public DevicesUserRequest(JSONObject object, Response.Listener<JSONArray> listener,
                          Response.ErrorListener errorListener) {
    super(sUrl, object, listener, errorListener);
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json");
    return headers;
  }
}
JChudasama commented 8 years ago

Same kind of issue facing on a simple StringRequest. Weird Problem.

JChudasama commented 8 years ago

@jfernandojr can you test by setting HEADERS in the following way

@Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String, String> params = new HashMap<>(); params.put("Content-Type", "application/x-www-form-urlencoded"); return params; }

or you need to Override the "getBodyContentType()" as you're using JSON request

jfernandojr commented 8 years ago

@JChudasama I messed up on DevicesUserRequest () method. The correct code is as follows:

public class DevicesUserRequest extends JsonArrayRequest {

private static final String Protocol = "http://";
private static final String Host = "192.241.251.229";
private static final String Port = "8080";
private static final String FullHost = Protocol + Host + ":" + Port;

//    private static final String sUrl = "http://gogps.com.br/gogps-rest/api/device/getDeviceUser";
private static final String sUrl = FullHost + "/Webservice/api/device/getLastInfDevice";

 public DevicesUserRequest(JSONObject object, Response.Listener<JSONArray> listener,
                           Response.ErrorListener errorListener) {
     super(sUrl, object, listener, errorListener);
  }

  @Override
  public Map<String, String> getHeaders() throws AuthFailureError {
      Map<String, String> headers = new HashMap<String, String>();
      headers.put("Content-Type", "application/json");
      return headers;
  }
 }

Notice that the only change was in the last url. Now I got the following error:

BasicNetwork.performRequest: Unexpected response code 401 for http://192.241.251.229:8080/Webservice/api/device/getLastInfDevice 02-15 20:35:18.754 11064-11064/? E/br.com.viprastreamento.rastreamento.ListaVeiculos: Look mistake here! com.android.volley.AuthFailureError at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:171) at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)

JChudasama commented 8 years ago

You need to authorise the request you're sending. Put your login credentials in Header Params.

You can also check on REST client 1st too.

jfernandojr commented 8 years ago

I left my head as follows:

   @Override
public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json");
    headers.put("imei", "459710040904196");
    headers.put("acao", "pos");
    return headers;
}

But the problem still remains.

JChudasama commented 8 years ago

Try something like this way

`@Override public Map<String, String> getHeaders() throws AuthFailureError { Map<String,String> headers = Constants.getHeaders(context); // add headers <key,value> String credentials = USERNAME+":"+PASSWORD; String auth = "Basic "

jfernandojr commented 8 years ago

thus does not satisfy me. I have a method that makes login and password to the server. This method returns me a key that was called IMEI.

So nessse process I want to do, I have to connect through the imai and the command as a parameter, being as follows:

   .put("imei", "459710040904196");
   .put("acao", "pos");

I managed to implement a class in eclipse using the same parameters and functioned normally.

But in Android Studio, I'm having trouble.