Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force close when try to Invoke method

I have a problem when I try to set up the client, I got the message: toString() failure in the factory.build. Method threw 'java.lang.IllegalArgumentException' exception. Cannot evaluate $Proxy1.toString() then when i call

ApiClientFactory factory = new ApiClientFactory();
factory.apiKey("xXxxXXXxxXXXXXXxxxxX");
final APIGatewayPediuClient client = factory.build(APIGatewayPediuClient.class); // exception happened in this line, proxy throw exepction

and when i try invoke the method

Places place = client.placesGet();

force close happens

com.amazonaws.http.UrlHttpClient.createHttpResponse(UrlHttpClient.java:72)
at com.amazonaws.http.UrlHttpClient.execute(UrlHttpClient.java:66)
at com.amazonaws.mobileconnectors.apigateway.ApiClientHandler.invoke(ApiClientHandler.java:91)
at java.lang.reflect.Proxy.invoke(Proxy.java:393)
at $Proxy1.placesGet(Unknown Source)
at net.devsoft.pediu.activity.HomeActivity$override.onCreate(HomeActivity.java:195)
at net.devsoft.pediu.activity.HomeActivity$override.access$dispatch(HomeActivity.java)`

SDK version:

compile com.amazonaws:aws-android-sdk-core:2.2.6'
compile 'com.amazonaws:aws-android-sdk-cognito:2.2.6'
compile 'com.amazonaws:aws-android-sdk-apigateway-core:2.2.6'
compile 'com.google.code.gson:son:1.7.2

but already tried with aws version 2.2.15 and still doesnt work, the SDK generate to Obj-C its working perfectly

like image 838
Thiago Falcão Avatar asked Apr 24 '16 17:04

Thiago Falcão


2 Answers

1 - Make sure to add Internet permission to your AndroidManifest.xml file:

<uses-permission android:name="android.permission.INTERNET" />

Note: uses-permission goes before application tag. i.e:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.awsapigatwway">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

2 - Make sure to execute your API Gateway call within an ASyncTask

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {
            ApiClientFactory factory = new ApiClientFactory();

            final PetStoreClient client = factory.build(PetStoreClient.class);

            Pet pet = client.petsPetIdGet("1");

            Log.d(pet.getType(), String.valueOf(pet.getPrice()));

            return null;
        }
    }.execute();
}

In this case, response should be seen on Logcat Debug.

like image 160
Victor R. Oliveira Avatar answered Oct 02 '22 06:10

Victor R. Oliveira


The solution is put this code inside a AsyncTask.

 new AsyncTask<Void, Void, Void>() {
        @Override
        protected Void doInBackground(Void... params) {

             ApiClientFactory factory = new ApiClientFactory();
             factory.apiKey("xXXxXXXXXXXxxxxXXxxXXXXxxx");
             final APIGatewayPediuClient client = factory.build(APIGatewayPediuClient.class); 
             Places place = client.placesGet();
            return null;
        }
    }.execute();
like image 45
Miguel Lemos Avatar answered Oct 02 '22 07:10

Miguel Lemos