javaandroidamazon-web-servicesaws-api-gatewayaws-sdk-android

How to get value from aws api gateway to android studio (java)


I dont understand why my output received by my android app from api gateway is like this dummypackage.model.Resultur@d0cce8e and not in string or json. how can i get some readable data?

I set up a dummy lambda function that sends a response

    return {
        statusCode: 200,
        headers: {
            "Access-Control-Allow-Origin": "*"
        },
        body:1
    }

when the api get method is called.

This is the model that i set up in the response body of my method response:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type":"object",
    "properties":{
        "output":{
            "$ref": "https://apigateway.amazonaws.com/restapis/3pzxzxz27/models/Output"
 
        }
    },
    "title":"Result"
}

The output model is

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type":"object",
    "properties":{
        "output":{
            "$ref": "https://apigateway.amazonaws.com/restapis/3pvgc7ic27/models/Output"
 
        }
    },
    "title":"Result"
}

The "resultur" class that is made in the sdk is:

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

And the 'output' class is this:

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

I used this code in my main activity to get my 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();

            }
        });

    }
}

What am i doing wrong? also what are some good places to learn how to use aws api sdk for android? (i followed all instructions of aws documentation but still got stuck)


Solution

  • In summary, you need to override the toString() method in your Resultur and Output classes, and use output.getOutput().toString() when displaying the toast message. For learning how to use the AWS API SDK for Android, you can refer to the https://aws-amplify.github.io/aws-sdk-android/docs/reference/