我不明白为什么我的android应用程序从API网关收到的输出是这样的dummypackage.model.Resultur@d0cce8e,而不是字符串或json。我怎么才能得到一些可读的数据?
我设置了一个发送响应的虚拟lambda函数
return {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*"
},
body:1
}
当调用API get方法时。
这是我在我的方法response的响应体中设置的模型:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type":"object",
"properties":{
"output":{
"$ref": "https://apigateway.amazonaws.com/restapis/3pzxzxz27/models/Output"
}
},
"title":"Result"
}
输出模型为
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type":"object",
"properties":{
"output":{
"$ref": "https://apigateway.amazonaws.com/restapis/3pvgc7ic27/models/Output"
}
},
"title":"Result"
}
在sdk中创建的“resultur”类是:
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package dummypackage.model;
import com.google.gson.annotations.SerializedName;
public class Resultur {
@SerializedName("output")
private Output output = null;
public Resultur() {
}
public Output getOutput() {
return this.output;
}
public void setOutput(Output output) {
this.output = output;
}
}
而“output”类是这样的:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package dummypackage.model;
import com.google.gson.annotations.SerializedName;
import java.math.BigDecimal;
public class Output {
@SerializedName("Output")
private BigDecimal output = null;
public Output() {
}
public BigDecimal getOutput() {
return this.output;
}
public void setOutput(BigDecimal output) {
this.output = output;
}
}
我在我的主活动中使用了以下代码来获取输出:
package com.example.apiapplication3;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.amazonaws.mobileconnectors.apigateway.ApiClientFactory;
import org.json.JSONObject;
import dummypackage.DummyapiClient;
import dummypackage.model.Resultur;
public class MainActivity extends AppCompatActivity {
Button button;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ApiClientFactory factory = new ApiClientFactory();
final DummyapiClient client = factory.build(DummyapiClient.class);
button = findViewById(R.id.button);
String values;
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
Resultur output = client.messageGet();
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, output, Toast.LENGTH_SHORT).show(); // Display the value in a Toast message
}
});
// Log.w("output", "s" + output);
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
});
}
}
我做错了什么?还有,有哪些好地方可以学习如何使用AWS API SDK for Android?(我遵循了AWS文档的所有说明,但仍然卡住了)
1条答案
按热度按时间13z8s7eq1#
总之,您需要在Resultur和Output类中覆盖toString()方法,并在显示吐司消息时使用output.getOutput().toString()。要了解如何使用AWS API SDK for Android,您可以参考https://aws-amplify.github.io/aws-sdk-android/docs/reference/