Android 在jsonArrayRequest中响应后,arrayList变为null

Android 在jsonArrayRequest中响应后,arrayList变为null,android,Android,错误:在BackgroundTask中,我在try块中获取arraylist中的数据,但在到达after errorListener()之后,我的arraylist变为null。在LogCat中我发现了这些问题。我如何解决这个问题 package com.example.rahul.volley_jarray; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.su

错误:在BackgroundTask中,我在try块中获取arraylist中的数据,但在到达after errorListener()之后,我的arraylist变为null。在LogCat中我发现了这些问题。我如何解决这个问题

package com.example.rahul.volley_jarray;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import java.util.ArrayList;

public class DisplayList extends AppCompatActivity {

    RecyclerView recyclerView;
    RecyclerView.Adapter adapter;
    RecyclerView.LayoutManager layoutManager;
    ArrayList<Contact> arrayList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_list);

        recyclerView= (RecyclerView) findViewById(R.id.recylerView);
        layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);

        BackgroundTask backgroundTask=new BackgroundTask(DisplayList.this);
        arrayList=backgroundTask.getList();

        Log.d("dispaly Array List",""+arrayList.toString());

        adapter=new RecyclerAdapter(arrayList);
        Log.d("My adapter",arrayList.toString());
        recyclerView.setAdapter(adapter);

    }
}
package com.example.rahul.volley\u jarray;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.support.v7.widget.LinearLayoutManager;
导入android.support.v7.widget.RecyclerView;
导入android.util.Log;
导入java.util.ArrayList;
公共类显示列表扩展了AppCompatActivity{
回收视图回收视图;
RecyclerView.适配器;
RecyclerView.LayoutManager LayoutManager;
ArrayList ArrayList=新的ArrayList();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u display\u list);
recyclerView=(recyclerView)findViewById(R.id.recylerView);
layoutManager=新的LinearLayoutManager(此);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
BackgroundTask BackgroundTask=新的BackgroundTask(DisplayList.this);
arrayList=backgroundTask.getList();
Log.d(“dispaly数组列表”,“+arrayList.toString());
适配器=新的回收适配器(arrayList);
Log.d(“我的适配器”,arrayList.toString());
recyclerView.setAdapter(适配器);
}
}
错误:在这个地方。背景任务

package com.example.rahul.volley_jarray;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by rahul on 7/4/2016.
 */
public class BackgroundTask {

    Context context;
    ArrayList<Contact> arrayList=new ArrayList<>();
    String str_url="https://raw.githubusercontent.com/ianbar20/JSON-Volley-Tutorial/master/Example-JSON-Files/Example-Array.JSON";
    String data="";
    public BackgroundTask(Context context)
    {
        this.context=context;
    }
    public ArrayList<Contact> getList()
    {
        final JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(Request.Method.GET, str_url, (String) null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {

                Log.d("My Json",response.toString());


                    int count = 0;
                    while (count < response.length()) {
                        try {
                            JSONObject jsonObject = response.getJSONObject(count);
                            JSONObject jsonObject1 = jsonObject.getJSONObject("phone");
                            Contact contact = new Contact(jsonObject.getString("name"), jsonObject.getString("email"), jsonObject1.getString("home"));
                            Log.d("contact", "" + contact.toString());
                            arrayList.add(contact);
                            Log.d("arrayList" + count, "" + arrayList.toString());
                            count++;


                        } catch (JSONException e) {
                            e.printStackTrace();
                            Log.d("Mytag", e.toString());
                        }
                        Log.d("arrayList in while" + count, "" + arrayList.toString());

                        Log.d("arrayList2" + count, "" + arrayList.toString());
                        if (arrayList.toString() == null) {
                            Log.d("if first", "null");
                        } else {
                            Log.d("else first", "not null");
                        }

                    }
            }
        },

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

                Toast.makeText(context,"Error...!!!",Toast.LENGTH_SHORT).show();
                error.printStackTrace();
            }
        });

        Log.d("final arrayList",""+arrayList.toString());
        //return arrayList;
        if(arrayList.toString()==null)
        {
            Log.d("if second","null");
        }
        else {
            Log.d("else second","not null");
        }

        Log.d("JsonArray Request",""+jsonArrayRequest.toString());

        MySingleton.getInstance(context).addToRequestQueue(jsonArrayRequest);
//
        return arrayList;
    }
}
package com.example.rahul.volley\u jarray;
导入android.content.Context;
导入android.util.Log;
导入android.widget.Toast;
导入com.android.volley.Request;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.JsonArrayRequest;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.util.ArrayList;
/**
*由rahul于2016年7月4日创建。
*/
公开课背景任务{
语境;
ArrayList ArrayList=新的ArrayList();
字符串str_url=”https://raw.githubusercontent.com/ianbar20/JSON-Volley-Tutorial/master/Example-JSON-Files/Example-Array.JSON";
字符串数据=”;
公共背景任务(上下文)
{
this.context=context;
}
公共ArrayList getList()
{
final JsonArrayRequest JsonArrayRequest=new JsonArrayRequest(Request.Method.GET,str_url,(String)null,new Response.Listener(){
@凌驾
公共void onResponse(JSONArray响应){
Log.d(“MyJSON”,response.toString());
整数计数=0;
while(count
//迈辛顿班 包com.example.rahul.volley_jarray

import android.content.Context;
import android.util.Log;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

/**
 * Created by rahul on 7/4/2016.
 */
public class MySingleton {

    private static MySingleton mInstance;
    private static Context mCtx;
    private RequestQueue requestQueue;

    private MySingleton(Context context) {
        mCtx = context;
        requestQueue = getRequestQueue();
        Log.d("request queue", "" + requestQueue.toString());
    }

    public RequestQueue getRequestQueue() {
        if (requestQueue == null) {
            requestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
        }
        return requestQueue;
    }

    public static synchronized MySingleton getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new MySingleton(context);
        }
        Log.d("mInstaces", "" + mInstance);
        return mInstance;
    }

    public <T> void addToRequestQueue(Request<T> request) {
        Log.d("request",""+request.toString());
        requestQueue.add(request);
        Log.d("now request queue",""+requestQueue.toString());
    }
}
导入android.content.Context;
导入android.util.Log;
导入com.android.volley.Request;
导入com.android.volley.RequestQueue;
导入com.android.volley.toolbox.volley;
/**
*由rahul于2016年7月4日创建。
*/
公共课米辛格尔顿{
私有静态MySingleton-mInstance;
私有静态上下文mCtx;
私有请求队列请求队列;
private MySingleton(上下文){
mCtx=上下文;
requestQueue=getRequestQueue();
Log.d(“请求队列”,“请求队列+requestQueue.toString());
}
公共请求队列getRequestQueue(){
if(requestQueue==null){
requestQueue=Volley.newRequestQueue(mCtx.getApplicationContext());
}
返回请求队列;
}
公共静态同步MySingleton getInstance(上下文){
if(minInstance==null){
MinInstance=新的MySingleton(上下文);
}
Log.d(“分钟数”和“+分钟数);
回报率;
}
公共无效addToRequestQueue(请求){
Log.d(“请求”,“请求+request.toString());
添加(请求);
Log.d(“现在请求队列”,“请求队列+requestQueue.toString());
}
}

Volley执行异步请求。当您检查arrayList时,它尚未完成(a
//define this interface in background task and implement it in caller activity
public interface CustomReqFinished(){
    public void onCustomReqFinished(ArrayList list){}
}

//in backgroundtask constructor store the activity you pass as follows
listener = (CustomReqFinished) activity; //set Activity as param instead of context
//in onResponse do this as last step
listener.onCustomReqFinished(arrayList);
public class DisplayList extends AppCompatActivity implements CustomReqFinished {

    RecyclerView recyclerView;
    RecyclerView.Adapter adapter;
    RecyclerView.LayoutManager layoutManager;
    ArrayList<Contact> arrayList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_list);

        recyclerView= (RecyclerView) findViewById(R.id.recylerView);
        layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);

        BackgroundTask backgroundTask=new BackgroundTask(this);
        backgroundTask.getList();
    }

    @Override
    public void onCustomReqFinished(ArrayList<Contact> list) {
        Log.d("dispaly Array List",""+list.toString());

        adapter=new RecyclerAdapter(list);
        recyclerView.setAdapter(adapter);
    }
}
public class BackgroundTask {

    public interface CustomReqFinished{
        public void onCustomReqFinished(ArrayList<Contact> list);
    }

    Context context;
    CustomReqFinished listener;
    ArrayList<Contact> arrayList=new ArrayList<>();
    String str_url="https://raw.githubusercontent.com/ianbar20/JSON-Volley-Tutorial/master/Example-JSON-Files/Example-Array.JSON";
    String data="";
    public BackgroundTask(Activity activity)
    {
        this.context = activity;
        this.listener = (CustomReqFinished) activity;

    }
    public void getList()
    {
        final JsonArrayRequest jsonArrayRequest=new JsonArrayRequest(str_url, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {

                Log.d("My Json",response.toString());


                    int count = 0;
                    while (count < response.length()) {
                        try {
                            JSONObject jsonObject = response.getJSONObject(count);
                            JSONObject jsonObject1 = jsonObject.getJSONObject("phone");
                            Contact contact = new Contact(jsonObject.getString("name"), jsonObject.getString("email"), jsonObject1.getString("home"));
                            Log.d("contact", "" + contact.toString());
                            arrayList.add(contact);
                            Log.d("arrayList" + count, "" + arrayList.toString());
                            count++;


                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    listener.onCustomReqFinished(arrayList);
            }
        },

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

                Toast.makeText(context,"Error...!!!",Toast.LENGTH_SHORT).show();
                error.printStackTrace();
            }
        });

        MySingleton.getInstance(context).addToRequestQueue(jsonArrayRequest);
    }
    }