https - 机器人香料。 RequestListener 不会触发

标签 https robospice

我正在使用 RoboSpice 接收和处理来自服务器的 JSON 响应。我在 RoboSpice 教程中做了所有的事情:在 AndroidManifest.xml 中添加了服务(NetworkSpiceService),添加了需要的库,实现了一个特殊的响应对象 - SimpleResponse with @JsonIgnoreProperties(ignoreUnknown = true)。

基础事件:

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

}

NetwokManager 类:

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 请求?

最佳答案

在哪里调用 private void load()?如果它是 ClickListener 的一部分,你应该没问题。如果它是 onCreate() 中的一个方法,您可能会遇到问题,因为 RoboSpice 尚未“启动”并且未绑定(bind),因此您的请求可能会排队等待执行并在没有引用您的监听器的情况下被忽略或返回。

尝试在您的 SpiceService 实现中覆盖 protected RequestListenerNotifier createRequestRequestListenerNotifier() 并在您的 DefaultRequestListenerNotifier 实现中抛出一堆断点进行调试以检查通话情况。

关于https - 机器人香料。 RequestListener 不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18122852/

相关文章:

apache - 尝试在 apache2 中将 http 重定向到 https

使用自签名证书和 CA 的 Android SSL HTTP 请求

java - 如何将数据发布到其他服务器中的表单

android - 如何正确使用com.octo.android.robospice来缓存位图

ssl - Visual Studio 2017 启用 SSL

wordpress - 如何在 WordPress 中将图片的 url 从 http 重写为 https?

android - Robospice : CachedSpiceReques. 未找到 getSpiceRequest

android - 如何使用 RoboSpice 下载 html 源代码?

android - Jackson - 无法序列化数组,UnknownLengthHttpInputStream

android - 通过在改造中透明地发送另一个请求来处理特定错误