返回对象 - OnPostExecute

时间:2016-06-16 09:53:25

标签: java android

我尝试使用此函数返回对象数组:

  public static JSONEvent[] invokeFunction(String funName, String requestContent) {

        final String functionName = funName;
        final String requestPayload = requestContent;

        new AsyncTask<Void, Void, InvokeResult>() {
            @Override
            protected InvokeResult doInBackground(Void... params) {
                try {
                    final ByteBuffer payload =
                            ENCODER.encode(CharBuffer.wrap(requestPayload));

                    final InvokeRequest invokeRequest =
                            new InvokeRequest()
                                    .withFunctionName(functionName)
                                    .withInvocationType(InvocationType.RequestResponse)
                                    .withPayload(payload);

                    final InvokeResult invokeResult =
                            AWSMobileClient
                                    .defaultMobileClient()
                                    .getCloudFunctionClient()
                                    .invoke(invokeRequest);

                    return invokeResult;
                } catch (final Exception e) {
                    Log.e("LAMBDA", "AWS Lambda invocation failed : " + e.getMessage(), e);
                    final InvokeResult result = new InvokeResult();
                    result.setStatusCode(500);
                    result.setFunctionError(e.getMessage());
                    return result;
                }
            }

            @Override
            protected void onPostExecute(final InvokeResult invokeResult) {

                try {
                    final int statusCode = invokeResult.getStatusCode();
                    final String functionError = invokeResult.getFunctionError();
                    final String logResult = invokeResult.getLogResult();

                    if (statusCode != 200) {
                        //showError(invokeResult.getFunctionError());
                    } else {
                        final ByteBuffer resultPayloadBuffer = invokeResult.getPayload();

                        //resultPayloadBuffer.rewind();
//                        while (resultPayloadBuffer.hasRemaining())
//                            Log.e("BUFFER",resultPayloadBuffer.position() + " -> " + resultPayloadBuffer.get());

//                        User a = new User(23, 24);
//
//                        User b = new User(58, 59);
//                        User[] ab = new User[] {a, b};

                       // User [] events = new User[3];

                        ObjectMapper mapper = new ObjectMapper();





                        final String resultPayload = DECODER.decode(resultPayloadBuffer).toString();
                        Log.e("LAMBDA-SUCCESS", resultPayload);
                        try {
                           // String s2 = getJson2(ab);
                           // Log.e("S2", s2);
                            //User[] user2 = mapper.readValue(resultPayload, User[].class);
                            events = mapper.readValue(resultPayload, JSONEvent[].class);

//                            for (JSONEvent u : events)
//                                Log.e("USER",u.getLocationLat()+"");

                            Log.e("ARRAY",Arrays.toString(events));

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

                        //return resultPayload;
                        // mResultField.setText(resultPayload);
                    }

                    if (functionError != null) {
                        Log.e("LAMBDA", "AWS Lambda Function Error: " + functionError);
                    }

                    if (logResult != null) {
                        Log.d("LAMBDA", "AWS Lambda Log Result: " + logResult);
                    }
                }
                catch (final Exception e) {
                    Log.e("LAMBDA", "Unable to decode results. " + e.getMessage(), e);
                    //showError(e.getMessage());
                }
            }
        }.execute();

        return events;
    }

问题是我在不同的活动中调用invokeFunction并返回null但是在onPostExecute中,数组不为null。它似乎在调用OnPostExecute之前返回数组。怎么解决?

2 个答案:

答案 0 :(得分:0)

问题是方法invokeFunction在onPostExecute之前完成(异步) 您可以使用接口来通信AsyncTask和活动。

接口(伪代码):

public interface AsyncCom {
    public void sendUsers(User [] events);
}

你的asynFunction(pseucode):

public void invokeFunction(String funName, String requestContent, AsyncCom listener) {
    ...

在postExecute(伪代码)中调用监听器的函数:

protected void onPostExecute(final InvokeResult invokeResult) {
    ...

    listener.sendUsers(events);

    }

在您的活动中声明接口并使用侦听器调用您的方法(伪代码):

public class MyActivity implements AsyncCom {

    ...

    invokeFunction(funName, requestContent, this);

    ...

最后,在您的活动中,实现返回的方法(伪代码):

public void sendUsers(User [] events){
    // do wathever you want with users
}

但请记住,这个回应将是异步的

答案 1 :(得分:0)

 private void invokeFunction(String funName, String requestContent{
    YourTask task = new YourTask();
    task.execute(new String[]{funName, requestContent});
}

static class YourTask extends AsyncTask<String, Void, InvokeResult> { 

    @Override
    protected InvokeResult doInBackground(String... params) {

        String funName = params[0];
        String requestContent = params[1];

       // ... 
    }

    @Override
    protected void onPostExecute(final InvokeResult invokeResult) {

       ///  . .. 
        doWhatYouNeedWithTheResult(result);
    }
    };
}