Java OkHttp异步调用返回JsonArray Null

Java OkHttp异步调用返回JsonArray Null,java,android,okhttp,Java,Android,Okhttp,我在okHttpCallback函数中声明了一个要返回的全局JSONArray变量,但它返回null。我正在获取数据,但返回时它为空 JSONArray jsonArray; //Global in class public JSONArray getJsonString(String link){ okHttpClient.newCall(request).enqueue(new Callback() { @Override

我在okHttpCallback函数中声明了一个要返回的全局JSONArray变量,但它返回null。我正在获取数据,但返回时它为空

JSONArray jsonArray; //Global in class

public JSONArray getJsonString(String link){

            okHttpClient.newCall(request).enqueue(new Callback() {

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

                    if(response.isSuccessful()){
                        try {

                            jsonArray = new JSONArray(response.body().string());

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

                    }else{
                        Log.d("ERROR", "onResponse: ERROR" + response.body().string());
                    }
                }
            });


       return jsonArray; // Null Here

    }

实际上,网络调用发生在另一个线程中,您正在主线程中返回
jsonArray
。只有在通过okhttp获得响应时,才应该返回jsonArray。 你应该做以下几点:-

public void getJsonResponse(String link){

            okHttpClient.newCall(request).enqueue(new Callback() {

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

                    if(response.isSuccessful()){
                        try {

                            jsonArray = new JSONArray(response.body().string());
                            getJsonString(jsonArray);

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

                    }else{
                        Log.d("ERROR", "onResponse: ERROR" + response.body().string());
                    }
                }
            });


    }

  // somewhere in class 

public JSONArray getJsonString(JSONArray jsonArr)
{
   return jsonArr;
}

是'jsonArray=newjsonarray(response.body().string());'是否有价值?将响应打印在logcat@sree有value@Swamy,后台任务似乎仍在运行,您返回的
jsonArray
为空。后台任务尚未完成completed@sreeonResponse是一个void方法,您在哪里返回JSONArray,getJsonResponse是void,您确定您的响应是json数组格式而不是对象格式吗????