gpt4 book ai didi

java - 返回对象-OnPostExecute

转载 作者:太空宇宙 更新时间:2023-11-04 12:30:52 26 4
gpt4 key购买 nike

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

  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;
}

问题是我在不同的 Activity 中调用 invokeFunction 并且它返回 null,但在 onPostExecute 中数组不为 null。看起来它在调用 OnPostExecute 之前返回数组。怎么解决?

最佳答案

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

接口(interface)(伪代码):

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

你的asynFunction(伪代码):

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

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

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

listener.sendUsers(events);

}

在您的 Activity 中声明接口(interface)并使用监听器调用您的方法(伪代码):

public class MyActivity implements AsyncCom {

...

invokeFunction(funName, requestContent, this);

...

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

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

但请记住,响应将是异步的

关于java - 返回对象-OnPostExecute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37855764/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com