我正在使用 RoboSpice 接收和处理来自服务器的 JSON 应答。我在 RoboSpice 教程中所做的一切都像:在 AndroidManifest.xml 中添加了服务(NetworkSpiceService),添加了必要的库,实现了一个特殊的响应对象 - 带有 @JsonIgnoreProperties(ignoreUnknown = true)的 SimpleResponse。
基础活动:
public abstract class BaseActivity extends Activity {
private SpiceManager spiceManager = new SpiceManager(NetworkSpiceService.class);
@Override
protected void onStart() {
spiceManager.start(this);
super.onStart();
}
@Override
protected void onStop() {
spiceManager.shouldStop();
super.onStop();
}
public SpiceManager getSpiceManager() {
return spiceManager;
}
}
我的工作活动:
public class MainActivity extends BaseActivity implements OnClickListener{
private static final String JSON_CACHE_KEY = "some_json";
private Button button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ...
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void load() {
getSpiceManager().execute(NetworkManager.getJson(param), JSON_CACHE_KEY, DurationInMillis.ALWAYS_EXPIRED, new JsonRequestListener());
}
private class JsonRequestListener implements RequestListener<SimpleResponse> {
@Override
public void onRequestFailure(SpiceException spiceException) {
Log.e("Json", "failure: " + spiceException.getMessage());
}
@Override
public void onRequestSuccess(SimpleResponse simpleResponse) {
Log.i("Json", "response");
Log.i("Json", "response received. offset = " + simpleResponse.getOffset() + " timezone = " + simpleResponse.getTimeZone());
}
}
网络管理器类:
public class NetworkManager {
private static final String API_KEY = ".....";
private static final String GET_SOME_URL = "https://api.servername.com/" + API_KEY + "/";
public static GetRequest<SimpleResponse> getJson(double param) {
return new GetRequest<SimpleResponse>(SimpleResponse.class, GET_SOME_URL + param);
}
}
和 GetRequest:
public class GetRequest<T extends SimpleRequest> extends
SpringAndroidSpiceRequest<T> {
private Class<T> clazz;
private String url;
public GetRequest(Class<T> clazz, String url) {
super(clazz);
this.clazz = clazz;
this.url = url;
}
@Override
public T loadDataFromNetwork() throws Exception {
return getRestTemplate().getForObject(url, clazz);
}
因此,在调用 load() 之后,我预计会触发 OnRequestFailure(...) 或 onRequestSuccess(...) ,但什么也没有。我确信 loadDataFromNetwork 正在调用。在浏览器上,我收到对该地址的响应,但在我的应用程序中没有。
怎么了?可能是我忘记了什么?或者可能需要以其他方式处理 https 请求?