Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 机器人空间&x2B;谷歌HTTP客户端&x2B;GSON=无法将JSON解析为对象_Android_Json_Gson_Robospice_Google Http Client - Fatal编程技术网

Android 机器人空间&x2B;谷歌HTTP客户端&x2B;GSON=无法将JSON解析为对象

Android 机器人空间&x2B;谷歌HTTP客户端&x2B;GSON=无法将JSON解析为对象,android,json,gson,robospice,google-http-client,Android,Json,Gson,Robospice,Google Http Client,我使用RoboSpice与Google HTTP客户端&GSON的方式如下: 蜂房服务: public class ApiSpiceService extends GoogleHttpClientSpiceService { private static final int THREAD_COUNT = 3; @Override public CacheManager createCacheManager(Application application) throws

我使用RoboSpice与Google HTTP客户端&GSON的方式如下:

蜂房服务:

public class ApiSpiceService extends GoogleHttpClientSpiceService {

    private static final int THREAD_COUNT = 3;

    @Override
    public CacheManager createCacheManager(Application application) throws CacheCreationException {
        CacheManager cacheManager = new CacheManager();

        GsonObjectPersisterFactory gsonObjectPersisterFactory = new GsonObjectPersisterFactory(application);
        cacheManager.addPersister(gsonObjectPersisterFactory);


        return cacheManager;
    }

    @Override
    public int getThreadCount() {
        return THREAD_COUNT;
    }
}
请求:

public class InfoRequest extends GoogleHttpClientSpiceRequest<Contact> {

    private final String url;

    public InfoRequest() {
        super(Contact.class);

        this.url = "some-url/path.json";
    }

    @Override
    public Contact loadDataFromNetwork() throws Exception {
        HttpTransport httpTransport = new NetHttpTransport();
        HttpRequestFactory httpRequestFactory = httpTransport.createRequestFactory(new InfoHttpRequestInitializer());
        HttpRequest httpRequest = httpRequestFactory.buildGetRequest(new GenericUrl(url));
        httpRequest.setParser(new GsonFactory().createJsonObjectParser());

        return httpRequest.execute().parseAs(Contact.class);
    }

    private class InfoHttpRequestInitializer implements HttpRequestInitializer {

        @Override
        public void initialize(HttpRequest request) throws IOException {
        }
    }
}
基本活动:

public abstract class BaseActivity extends ActionBarActivity {

    protected SpiceManager spiceManager = new SpiceManager(ApiSpiceService.class);


    @Override
    protected void onStart() {
        super.onStart();

        spiceManager.start(this);
    }

    @Override
    protected void onStop() {
        spiceManager.shouldStop();

        super.onStop();
    }
}
和主要活动:

public class MainActivity extends BaseActivity {
   private InfoRequest infoRequest;

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        infoRequest = new InfoRequest();
    }

    @Override
    protected void onStart() {
        super.onStart();

        spiceManager.execute(infoRequest, "txt", DurationInMillis.ALWAYS_EXPIRED, new TextRequestListener());
    }

private class TextRequestListener implements RequestListener<Contact> {

        @Override
        public void onRequestFailure(SpiceException spiceException) {
           //
        }

        @Override
        public void onRequestSuccess(Contact s) {
            //
        }
    }
public类MainActivity扩展了BaseActivity{
私人信息请求信息请求;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
infoRequest=新的infoRequest();
}
@凌驾
受保护的void onStart(){
super.onStart();
execute(infoRequest,“txt”,DurationInMillis.ALWAYS_过期,new TextRequestListener());
}
私有类TextRequestListener实现RequestListener{
@凌驾
公共void onRequestFailure(SpiceException){
//
}
@凌驾
public void onRequestSuccess(联系人){
//
}
}
它似乎是有效的代码,但不幸的是,当它完成请求执行时,返回的Contact实例中的字段数据为null。 logcat中没有错误


请求的内容是100%有效的JSON。为什么不解析它?

好的,我找到了解决方案。我需要在模型中的字段中添加@Key注释。这很奇怪,因为纯Gson不需要这个

public class Contact {

    @Key
    private String data;

}
public class Contact {

    @Key
    private String data;

}