首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >接口不能得到回调,Android

问接口不能得到回调,Android
EN

Stack Overflow用户
提问于 2016-03-10 08:49:15
回答 2查看 66关注 0票数 1

我在另一堂课上写了定制的Volley Android。但我对此有意见。这是我的定制截击课:

代码语言:javascript
复制
public class CustomVolley {

    private Context _c;

    public OnCallbackResponse OnCallbackResponse;

    public interface OnCallbackResponse {
        void OnVolleyErrorResponse(String TAG, String response);
        void onVolleySuccessResponse(String TAG, String response);
    }

    public CustomVolley(Context c) {
        _c=c;
    }

    public RequestQueue Rest(int METHOD, String URL, final Map<String, String> jsonParams , final String TAG) {
        Log.v("URL "+TAG, URL);
        RequestQueue queue = Volley.newRequestQueue(_c);
        StringRequest sr = new StringRequest(METHOD, URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.v("onVolleySuccessResponse URL "+TAG, response);
                        if (OnCallbackResponse != null) {
                            OnCallbackResponse.onVolleySuccessResponse(TAG, response);
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                NetworkResponse response = error.networkResponse;
                String resp="error";
                if (response != null && response.data != null) {
                    resp = new String(response.data);
                }
                if (OnCallbackResponse != null) {
                    Log.v("OnVolleyErrorResponse URL "+TAG, resp);
                    OnCallbackResponse.OnVolleyErrorResponse(TAG, resp);
                }
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                return jsonParams;
            }
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<>();
                params.put("Content-Type", "application/x-www-form-urlencoded");
                return params;
            }
        };
        sr.setRetryPolicy(new DefaultRetryPolicy(
                Variabel.MY_SOCKET_TIMEOUT_MS_NEWS,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        sr.setShouldCache(false);
        sr.setTag(TAG);
        queue.add(sr);
        return queue;
    }
}

我把我的活动叫做:

代码语言:javascript
复制
public class GeneralBusinessActivity extends AppCompatActivity implements CustomVolley.OnCallbackResponse {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  
....

    context= getApplicationContext();
    customVolley = new CustomVolley(context);

    String url = "https://sds.com/api/search/detail/business?id_business=" + s_id_business + "&" + Variabel.app_key + "="
                + Variabel.value_app_key;
        queue_getDetailResultBusiness = customVolley.Rest(Request.Method.GET, url, null, "getDetailResultBusiness");

    }

    @Override
    public void OnVolleyErrorResponse(String TAG, String response) {
        Toast.makeText(GeneralBusinessActivity.this, response, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onVolleySuccessResponse(String TAG, String response) {
        Toast.makeText(GeneralBusinessActivity.this, response, Toast.LENGTH_SHORT).show();

    }

}

但这不是回叫onError或onSuccess..。那么如何解决呢?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-03-10 08:52:04

您必须分配对象,否则它将保持为空。您可以在CustomVolley类中添加一个setter。E.g

代码语言:javascript
复制
  public void setOnCallbackResponse(OnCallbackResponse l) {
     OnCallbackResponse = l;
  }

在你的活动中:

代码语言:javascript
复制
  customVolley.setOnCallbackResponse(this);

this作为setOnCallbackResponse的参数,因为在您的情况下,GeneralBusinessActivity正在实现接口

票数 1
EN

Stack Overflow用户

发布于 2016-03-10 09:07:42

试着改变这个:

代码语言:javascript
复制
public CustomVolley(Context c)
{
       _c=c;
}

到这个

代码语言:javascript
复制
 public CustomVolley(Context c)
 {
      _c=c;
      OnCallbackResponse = (OnCallbackResponse) c;
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35911540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档