使用Volley在android中获取JSON的不兼容类型

时间:2016-06-02 16:19:58

标签: android json android-studio android-volley

我正在创建一个应用程序,我正在使用Volley从API获取数据但在Request.Method.GET中它给出了一个错误,指出“不兼容的类型:int无法转换为String”。在这里你可以看到方法:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.JsonObject;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
* A simple {@link Fragment} subclass.
*/

public class NegociosFragment extends Fragment {

public RecyclerView recyclerView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

List<Empresas> empresas = rellenar();

RequestQueue requestQueue = Volley.newRequestQueue(getContext());

 // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_negocios,container,false);
    //1. get a reference to recyclerView
    recyclerView = (RecyclerView) view.findViewById(R.id.reciclador);
    //2. set layoutManager
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    //3. create an adapter
    EmpresasAdapter adapter = new EmpresasAdapter(getActivity(), /*Empresas2.getEmpresas()*/empresas);
    //4. set adapter
    recyclerView.setAdapter(adapter);
    //5. set item animator to DefaultAnimator
    recyclerView.setItemAnimator(new DefaultItemAnimator());

    return view;
}

private List<Empresas> rellenar() {

    final List<Empresas> empresas2 = null ;

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "my url of the API",
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray jsonArray = response.getJSONArray("results");

                        for(int i=0; i<jsonArray.length(); i++){
                            JSONObject objeto = jsonArray.getJSONObject(i);

                            int id = objeto.getInt("id");
                            String data1 = objeto.getString("data1");
                            String data2 = objeto.getString("data2");
                            String data3 = objeto.getString("data3");
                            String data4 = objeto.getString("data4");
                            String data5 = objeto.getString("data5");
                            String data6 = objeto.getString("data6");
                            String data7 = objeto.getString("data7");
                            String data8 = objeto.getString("data8");
                            String data9 = objeto.getString("data9");
                            Double double1 = objeto.getDouble("double1");
                            Double double2 = objeto.getDouble("double2");
                            String data10 = objeto.getString("data10");
                            String data11 = objeto.getString("data11");
                            String data12 = objeto.getString("data12");
                            String data13 = objeto.getString("data13");
                            String data14 = objeto.getString("data14");
                            String data15 = objeto.getString("data15");
                            String data16 = objeto.getString("data16");
                            String data17 = objeto.getString("data17");
                            String data18 = objeto.getString("data18");
                            String data19 = objeto.getString("data19");
                            String data20 = objeto.getString("data20");
                            String data21 = objeto.getString("data21");
                            String data22 = objeto.getString("data22");
                            String data23 = objeto.getString("data23");
                            String data24 = objeto.getString("data24");
                            String data25 = objeto.getString("data25");
                            String data26 = objeto.getString("data26");
                            String data27 = objeto.getString("data27");
                            //change null to default information
                            data1 = comprobarString(data1);
                            data2 = comprobarString(data2);
                            data3 = comprobarString(data3);
                            data4 = comprobarString(data4);
                            data5 = comprobarString(data5);
                            data6 = comprobarString(data6);
                            data7 = comprobarString(data7);
                            data8 = comprobarString(data8);
                            data9 = comprobarString(data9);
                            double1 = comprobarDouble(double1);
                            double2 = comprobarDouble(double2);
                            data10 = comprobarString(data10);
                            data11 = comprobarString(data11);
                            data12 = comprobarString(data12);
                            data13 = comprobarString(data13);
                            data14 = comprobarString(data14);
                            data15 = comprobarString(data15);
                            data16 = comprobarString(data16);
                            data17 = comprobarString(data17);
                            data18 = comprobarString(data18);
                            data19 = comprobarString(data19);
                            data20 = comprobarString(data20);
                            data21 = comprobarString(data21);
                            data22 = comprobarString(data22);
                            data23 = comprobarString(data23);
                            data24 = comprobarString(data24);
                            data25 = comprobarString(data25);
                            data26 = comprobarString(data26);
                            data27 = comprobarString(data27);




                            empresas2.add(new Empresas(id, data1, data2, data3,
                                    data4, data5, data6, data7,
                                    data8, data9, double1, double2, data10, data11,
                                    data12, data13, data14, data15, data16, data17, data18, data19,
                                    data20, data21, data22, data23, data24, data25, data26, data27));
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            },
      new Response.ErrorListener(){
          @Override
          public void onErrorResponse(VolleyError error){

          }
      }
    );
    requestQueue.add(jsonObjectRequest);
    return empresas2;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

Double comprobarDouble(Double num){
    Double fin = null;
        if(num==null){
            fin=0D;
        }
        else if(num!=null){
            fin=num;
        }
    return fin;
}
String comprobarString(String txt){
    String text;
        if (txt==null){
            text="No disponible";
        }
        else if(txt.equals("null")){
            text="No disponible";
        }
        else{
            text=txt;
        }

    return text;
}





}

这是Empresas的构造函数:

public class Empresas {
int id;
String data1;
String data2;
String data3;
String data4;
String data5
String data6;
String data7;
String data8;
String data9;
Double double1;
Double double2;
String data10;
String data11;
String data12;
String data13;
String data14;
String data15;
String data16;
String data17;
String data18;
String data19;
String data20;
String data21;
String data22;
String data23;
String data24;
String data25;
String data26;
String data27;


public Empresas(int id, String data1,String data2,String data3,String data4,String data5,String data6, String data7,
                String data8, String data9, Double double1, Double double2, String data10, String data11, String data12, String data13,
                String data14, String data15, String data16, String data17, String data18, String data19, String data20, String data21, String data22,
                String data23, String data24, String data25, String data26, String data27){
    this.id=id;
    this.data1=data1;
    this.data2=data2;
    this.data3=data3;
    this.data4=data4;
    this.data5=data5;
    this.data6=data6;
    this.data7=data7;
    this.data8=data8;
    this.data9=data9;
    this.double1=latitud;
    this.double2=longitud;
    this.data10=data10;
    this.data11=data11;
    this.data12=data12;
    this.data13=data13;
    this.data14=data14;
    this.data15=data15;
    this.data16=data16;
    this.data17=data17;
    this.data18=data18;
    this.data19=data19;
    this.data20=data20;
    this.data21=data21;
    this.data22=data22;
    this.data23=data23;
    this.data24=data24;
    this.data25=data25;
    this.data26=data26;
    this.data27=data27;

}
}

这是JSON的结构

{
status: "OK",
code: 0,
message: "",
results: [
 {
 id: 42,
 data1: "data",
 data2: "data",
 data3: "data",
 data4: "data",
 data5: "data",
 data6: "data",
 data7: "data",
 data8: "data",
 data9: "data",
 double1: double1,
 double2: double2,
 data10: "data",
 data11: "data",
 data12: "data",
 data13: "data",
 data14: "data",
 data15: "data",
 data16: "data",
 data17: "data",
 data18: "data",
 data19: "data",
 data20: "data",
 data21: "data",
 data22: "data",
 data23: "data",
 data24: "data",
 data25: "data",
 data26: "data",
 data27: "data"
 },

错误引发你的行 ACO file format

堆栈跟踪 The line that the error marks

这是新的错误: Stacktrace

enter image description here

1 个答案:

答案 0 :(得分:2)

您应该更改此行:

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "my url of the API",
        new Response.Listener<JSONObject>() {

对此:

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "my url of the API", null,
        new Response.Listener<JSONObject>() {

你错过了那里的参数。