Store Volley Request data

China☆狼群 提交于 2020-01-23 11:26:12

问题


I'm learning to program by myself and I am facing a problem which I believe is easy to solve.

I'm making requests using Google Volley and like to know how I store the information coming in the request for me to work with them in my onCreate.

public void parseJSON(){

StringRequest getRequest = new StringRequest(Request.Method.GET,activity.getString(R.string.URL),
            new Response.Listener() {

                @Override
                public void onResponse(String response) {
                  I need to store the data for use in the method onCreate.

            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    Log.e("Error.Response", error.toString());


                }
            });
    request.add(getRequest);
}

I do not know how to pass the data of this separate method to main.

I'm starting alone and has researched a lot, but found nothing with this simple problem.

Thank you anyway!


回答1:


You can use SharedPreferences to store the data. Create a method to do that.

private void sharedResponse(String response) {
    SharedPreferences m = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = m.edit();
    editor.putString("Response", response);
    editor.commit();
}

And call this method from your onResponse.

@Override
public void onResponse(String response) {
  //I need to store the data for use in the method onCreate.
  sharedResponse(response);
}

You can access this data from other class by

SharedPreferences m = PreferenceManager.getDefaultSharedPreferences(this);
mResponse = m.getString("Response", "");



回答2:


in onResponse method the data type of response is JSONArray or JSONObject, so what u have to do to get it in String is simply call response.toString() now u can return it in your parseJSON() like this:

public String parseJSON(){
 final String responseStr = new String();

        JsonArrayRequest request = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
responseStr = response.toString();
                for (int i = 0; i < response.length(); i++) {
                    try {
//parse you Json here

} catch (JSONException e) {
                        Log.d(e.toString());
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d(error.toString());
            }
        });

        Volley.newRequestQueue(context).add(request);

        return responseStr ;

    }

or you can make a public static function in you activity to set a String and call it in the parseJSON()



来源:https://stackoverflow.com/questions/32619035/store-volley-request-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!