如何实现aws api网关到android?

时间:2016-12-28 02:22:55

标签: android amazon-web-services aws-lambda aws-sdk aws-api-gateway

所以我部署了一个api并将sdk导入android。我试图执行一个简单的GET方法。在我输入userName的位置,它返回给用户lastName

调用lambda函数(后端函数),所以我能够连接到它,但我无法获得输出。

这是我的班级:

public class SampleGet extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in);

        gatewayButton = (Button) findViewById(R.id.cognito_gatewayButton);
        gatewayButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                gateWayAsyncTask gateWayAsyncTask = new gateWayAsyncTask();
                gateWayAsyncTask.execute();

            }
        });
    }

    class gateWayAsyncTask extends AsyncTask<Void, Void, Void>
    {
        private String userName;
        @Override
        protected Void doInBackground(Void... params)
        {
            //gateway
            creatingUser = clientFactory.build(MyUserClient.class);
            userName = creatingUser.rootGet("SampleUserName").getLastName;
            return null;
        }
        @Override
        public void onPostExecute(Void var)
        {
            Log.d("gateway","gateway succeded!");
            if( userName == null)
            {
                Log.d("gateway","username is null");
            }
            else if(userName.equals(""))
            {
                Log.d("gateway","username is empty");
            }
            else
            {
                Log.d("gateway",userName);
            }

        }
    }

在我从api网关生成的sdk中,这里是客户端类:

@com.amazonaws.mobileconnectors.apigateway.annotation.Service(endpoint = "https://ow2zhiry2b.execute-api.us-west-2.amazonaws.com/test5")

public interface MyUserClient {


    /**
     * A generic invoker to invoke any API Gateway endpoint.
     * @param request
     * @return ApiResponse
     */
    com.amazonaws.mobileconnectors.apigateway.ApiResponse execute(com.amazonaws.mobileconnectors.apigateway.ApiRequest request);

    /**
     * 
     * 
     * @param username 
     * @return AndroidOutput
     */
    @com.amazonaws.mobileconnectors.apigateway.annotation.Operation(path = "/", method = "GET")
    AndroidOutput rootGet(
            @com.amazonaws.mobileconnectors.apigateway.annotation.Parameter(name = "username", location = "query")
            String username);

}

这是AndroidOuput类,它也是生成的:

public class AndroidOutput {
    @com.google.gson.annotations.SerializedName("items")
    private String items = null;

    /**
     * Gets items
     *
     * @return items
     **/
    public String getItems() {
        return items;
    }

    /**
     * Sets the value of items.
     *
     * @param items the new value
     */
    public void setItems(String items) {
        this.items = items;
    }

}

所以出来的日志是username is null,我不明白为什么。我检查了cloudwatch,确实我的后端lambda函数正在运行。当我在网关中测试api时,它会在那里运行。如果有人能帮助我那太棒了!!

2 个答案:

答案 0 :(得分:1)

如果您熟悉Retrofit并且不想切换到AWS sdk,则可以考虑将AWS Gateway OkHttp Interceptor添加到OkHttpClient

答案 1 :(得分:0)

您是否考虑过使用AWS Gateway for Android