从volley onResponse返回值

时间:2016-05-11 02:29:47

标签: java android object android-volley

我正在尝试用Volley制作一个更面向对象的程序。目前我遇到的问题是无法从凌空的onResponse方法中提取数据。

 private void getMember(String memberid){
    Response.Listener<String> responseListener = new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonResponse = new JSONObject(response);
                boolean success = jsonResponse.getBoolean("success"); //key["success"]
                String userid = jsonResponse.getString("userid");
                if(success){
                    byte[] decoded_img = Base64.decode(jsonResponse.getString("userpic"), Base64.DEFAULT);
                    Bitmap member_pic = BitmapFactory.decodeByteArray(decoded_img, 0, decoded_img.length);
                    FamMember member = new FamMember(jsonResponse.getString("username")); // creating new object

////// where I need to capture the image/ any data that I get in the Json response///
                    AlertDialog.Builder builder = new AlertDialog.Builder(LandingActivity.this);
                    builder.setMessage(member.username)
                            .setNegativeButton("Retry",null)
                            .create()
                            .show(); //properly displays newly created object 'username'
                } else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(LandingActivity.this);
                    builder.setMessage("Unable to Login")
                            .setNegativeButton("Retry",null)
                            .create()
                            .show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };
    //HAPPENS AFTER RESPONSE HAS BEEN GIVEN FROM SERVER////////
    MemberRequest memberRequest = new MemberRequest(type,memberid,responseListener);
    RequestQueue queue  = Volley.newRequestQueue(LandingActivity.this);
    queue.add(memberRequest); //puts actual request into the queue for processing
}

我必须能够获得响应并将它们用作变量以传递给我的活动。当我收到上述方法无法获得任何回报的错误时,如何执行此操作?

4 个答案:

答案 0 :(得分:2)

创建一个像这样的新界面:

  public interface VolleyCallback {
            void onSuccess(String result);
            void onError(String result);
        }

使用回调接口的地方:

 public void getString(final VolleyCallback callback) {
      StringRequest strReq = new StringRequest(Request.Method.GET, url,
           new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                    callback.onSuccess(response);
            }
           }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
               callback.onError(volleyError + "");
            }
        });
        Volley.newRequestQueue(MyApplication.getAppContext()).add(strReq);
      }

在活动类上使用界面,例如:

 FunctionName(new VolleyCallback(){
         @Override
         public void onSuccess(String result){
             ... //do stuff here
         }
         @Override
         public void onError(String result) {
             ... //show error here         
         }
    });

答案 1 :(得分:1)

在Volley块之外创建全局变量

    public String response_var="";

    public void getMember()
    {
    @Override
    public void onResponse(String response) {
    {
 response_var=response;
    }

    }

答案 2 :(得分:1)

Volley和其他异步HTTP库很好地异步。您的活动无法直接获取http响应。您需要使用onResponse方法处理数据,然后与活动进行通信。一种方法是发送本地广播。

答案 3 :(得分:1)

您可以创建一个方法,将响应(或您想要的部分响应)作为参数。例如:

0.5

然后,您可以从onResponse方法中传递对此方法的响应:

private void processResponse(String encodedUserPic, String userName) {

    // Use the response as you need
    byte[] decodedImg = Base64.decode(encodedUserPic), Base64.DEFAULT);
    Bitmap memberPic = BitmapFactory.decodeByteArray(decodedImg, 0, decodedImg.length);
    FamMember member = new FamMember(userName); 
}

保证金说明:在Java中,较低的camelcase表示法是惯例,而不是强调。

希望你觉得这很有用!