java中返回null的接口

java中返回null的接口,java,Java,我试图在Android应用程序中从AsyncHttpClient获取信息,我需要使用一个接口来设置变量,以便在我的主方法中使用它。但是,当我运行System.out.println(PostResponse)时,我得到的是null 我不明白为什么,因为如果我在callback()方法中放入该行,就会得到值 根据我的主要方法: try { JSONArray PostResponse = PerformPostRequest(new OnJSONResponseCallback() {

我试图在Android应用程序中从AsyncHttpClient获取信息,我需要使用一个接口来设置变量,以便在我的主方法中使用它。但是,当我运行
System.out.println(PostResponse)时,我得到的是
null

我不明白为什么,因为如果我在
callback()
方法中放入该行,就会得到值

根据我的主要方法:

try {
    JSONArray PostResponse = PerformPostRequest(new OnJSONResponseCallback() {
        @Override
        public JSONArray onJSONResponse(boolean success, JSONArray response) {
            System.out.println("Response: " + response);  //This is returning the correct value

            return response;
        }
    }, PostData);

    System.out.println("Useable: " + PostResponse); //This is returning null.
} catch (Exception e) {
    e.printStackTrace();
}
界面:

public interface OnJSONResponseCallback {
    public JSONArray onJSONResponse(boolean success, JSONArray response);
}
AsyncHttpClient:

public JSONArray PerformPostRequest(final OnJSONResponseCallback callback, JSONObject PostData) {
    //To authenticate against the API we need the user's credentials
    String Email = getSharedPreferences(ctx).getString("Email","");
    String Password = getSharedPreferences(ctx).getString("Password","");

    final JSONArray[] ResponseStorage = new JSONArray[1];

    //Add the credentials to post data
    try{
        PostData.put("email", Email);
        PostData.put("password", Password);
    } catch (Exception e){
        e.printStackTrace();
    }

    //Then we need to put the post data into request parameters so we can send them in the call.
    RequestParams RequestParameters = new RequestParams();
    RequestParameters.put("data", PostData);

    //This is the client we will use to make the request.
    AsyncHttpClient client = new AsyncHttpClient();

    client.post(AppHost + "MyMeetings.php", RequestParameters, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            try {
                String ResponseString = new String(responseBody);
                ResponseStorage[0] = new JSONArray(ResponseString);
                System.out.println(ResponseStorage[0] + "<=============");  //Returns with the array
                callback.onJSONResponse(true, ResponseStorage[0]);
            } catch (Exception e) {
                Log.e("Exception", "JSONException on success: " + e.toString());
            }
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
            try {
                Toast.makeText(ctx, "Error: " + statusCode, Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Log.e("Exception", "JSONException on failure: " + e.toString());
            }
        }
    });

    JSONArray ResponseArray = new JSONArray();

    try{
        System.out.println(ResponseStorage[0] + "<==============="); //Returning null?
        ResponseArray = ResponseStorage[0];
    } catch (Exception e){
        e.printStackTrace();
    }

    System.out.println("ResponseArray" + ResponseArray); //Returns null
    return ResponseArray;
}
publicJSONArray性能请求(最终OnJSONResponseCallback回调,JSONObject PostData){
//要根据API进行身份验证,我们需要用户的凭据
字符串Email=getSharedReferences(ctx).getString(“Email”,”);
字符串密码=GetSharedReferences(ctx).getString(“密码”,即“);
最终JSONArray[]响应范围=新JSONArray[1];
//添加凭证以发布数据
试一试{
PostData.put(“电子邮件”,email);
PostData.put(“密码”,password);
}捕获(例外e){
e、 printStackTrace();
}
//然后我们需要将post数据放入请求参数中,以便在调用中发送它们。
RequestParams RequestParameters=新的RequestParams();
RequestParameters.put(“数据”,PostData);
//这是我们将用于发出请求的客户端。
AsyncHttpClient=新的AsyncHttpClient();
client.post(AppHost+“MyMeetings.php”、RequestParameters、新的AsyncHttpResponseHandler(){
@凌驾
成功时的公共void(int statusCode,Header[]headers,byte[]responseBody){
试一试{
字符串ResponseString=新字符串(responseBody);
ResponseStoreRage[0]=新JSONArray(ResponseString);

System.out.println(ResponseStorage[0]+“异步调用背后的一般思想是:

  • 异步方法调用(在您的例子中是
    PerformPostRequest
    )立即返回,并且不返回预期的结果-相反,它只返回一个接受确认或一个对象,您将来有时可以从中获得结果(例如
    future
    的实例)

  • 您为该方法提供回调接口(在您的例子中是
    OnJSONResponseCallback
    ),或者该方法返回回调接口的实例,并定期检查是否已准备好结果

您不应该期望异步方法立即返回结果,这与异步调用正好相反

这是用图片表达的大致想法。它只是整个想法的一个整体,所以实现细节可能会有很大的不同


我试图从
PerformPostRequest()
设置变量,默认情况下不会调用该变量。在类的顶部,我设置了一个

public JSONArray[] PostResponse = new JSONArray[1];
并将我调用post请求的位置更新为:

//Make a post request
try {
    JSONObject PostData = new JSONObject();
    PostData.put("action","test");
    PerformPostRequest(new OnJSONResponseCallback(){
        @Override
        public JSONArray onJSONResponse(boolean success, JSONArray response) {
            PostResponse[0] = response;
            System.out.println(PostResponse[0]); //This will be replaced by calling the method I want to call.
            return PostResponse[0];
        }
    }, PostData);
    System.out.println(PostResponse[0]);
} catch (Exception e){
    e.printStackTrace();
}

我猜,您正试图在响应实际返回之前对其进行处理。
PerformPostRequest
的最后一行是
return null;
。它返回
null
,因为您告诉它。只有一个
return
语句,它返回
null
。我试图返回
ResponseArray
(在onsuccess中设置)但我无法从onsuccess返回它,因为它是public void。当我尝试将其更改为public JSONArray时,我得到一个不兼容的返回类型错误。