2015-04-04 117 views
1

我使用RoboSpice與谷歌HTTP客戶端& GSON這樣:RoboSpice +谷歌HTTP客戶端+ GSON =無法解析JSON反對

ApiSpiceService:

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 { 
     } 
    } 
} 

型號(Contact.java):

public class Contact { 
    private String data; 
} 

BaseActivity:

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(); 
    } 
} 

而且MainActivity:

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) { 
      // 
     } 
    } 

這似乎是有效的代碼,但不幸的是,當它完成的請求執行,在返回的聯繫人實例字段的數據爲空。 logcat中沒有錯誤。

請求的內容是100%有效的JSON。爲什麼它不被解析?

回答

1

好的,我找到了解決方案。我需要爲模型中的字段添加@Key註釋。這很奇怪,因爲純Gson不需要這個。

public class Contact { 

    @Key 
    private String data; 

}