java.lang.IllegalStateException:预期为BEGIN_ARRAY,但在第1行第2行路径为$ BEGIN_OBJECT

时间:2015-05-27 23:48:16

标签: java android retrofit

我已在portal.exosite上注册,我添加了一些数据设备。我正在尝试使用改造来获取此数据并通过身份验证发送GET请求,但我收到了标题中的错误消息。

我的界面:

import java.util.List;

import retrofit.Callback;
import retrofit.http.GET;

public interface RetroInterface{

    @GET("/api/portals/v1/data-sources/0e948702d54c4b11fa449cbd9c9a197ba0c664b6")
    public void getData(Callback<List<Data>> response);


}

class获取数据和单位的数据:

public class Data {

    private String data;
    private String unit;

    public String getFetchedData(){  return data;}

    public void putData(String data){ this.data = data; }

    public String getFetchedUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }
}

和请求:

public static final String ENDPOINT =
            "https://portals.exosite.com";

    List<Data> dataList;


    private void requestData(){

        OkHttpClient client = new OkHttpClient();
        String username = "my_email";
        String password = "my_password";

        RestAdapter.Builder builder = new  RestAdapter.Builder()
                .setEndpoint(ENDPOINT)
                .setClient(new OkClient(client));

        final String credentials = username + ":" + password;
        builder.setRequestInterceptor(new RequestInterceptor() {
            @Override
            public void intercept(RequestFacade request) {
                String string = "Basic " + Base64.encodeToString(
                        credentials.getBytes(),
                        Base64.NO_WRAP
                );
                request.addHeader("Accept","application/json");
                request.addHeader("Authorization", string);
            }
        });

        RestAdapter adapter = builder.build();


        RetroInterface api = adapter.create(RetroInterface.class);

        api.getData(new Callback<List<Data>>(){

            @Override
            public void success(List<Data> arg0, Response arg1){

                dataList = arg0;
                Toast.makeText(MainActivity.this,"Data successfully fetched ",Toast.LENGTH_LONG).show();


            }
            @Override
            public void failure(RetrofitError arg0){

                String erro = arg0.toString();
                Toast.makeText(MainActivity.this,erro,Toast.LENGTH_LONG).show();
                Log.e(LOG_TAG,erro);
            }
        });
    }

并且这应该从exosite回复:

{"data":[[1432735517,19]],"info":{"basic":{"modified":1430694552,"subscribers":0,"type":"dataport"},"description":{"format":"float","meta":"{\"datasource\":{\"description\":\"\",\"unit\":\"C\"}}","name":"Temperature","preprocess":[],"public":false,"retention":{"count":"infinity","duration":"infinity"},"subscribe":null},"shares":[],"storage":{"count":4,"first":1430694960,"last":1432735517,"size":48},"subscribers":[],"tags":[]},"rid":"0e948702d54c4b11fa449cbd9c9a197ba0c664b6","unit":"C"}

我希望得到这个回复“数据”和“单位”。我是android的新手,我不知道是什么问题...

1 个答案:

答案 0 :(得分:0)

所以从我读过的内容来看,你不能使用List,因为它不是列表而是对象。改变它,它应该正常运作。

我认为这与某个需要调用数组的方法有关,但是发送它的响应对象更像是一个类似于JSON对象的“字典”。