Я новичок в Android Studio и следую этому руководству. AWS Tutorial . Я внес некоторые изменения, так как не хочу копировать и вставлять точный код из руководства. Как уже говорилось, я получаю эту ошибку при нажатии кнопки приложения в моей студии Android:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.amazonaws.youruserpools.CognitoYourUserPoolsDemo, PID: 14152
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1513)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:117)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:105)
at java.net.InetAddress.getAllByName(InetAddress.java:1154)
at com.android.okhttp.Dns$1.lookup(Dns.java:39)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:175)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:141)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:83)
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:127)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:258)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getOutputStream(DelegatingHttpsURLConnection.java:218)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:26)
at com.amazonaws.http.UrlHttpClient.writeContentToConnection(UrlHttpClient.java:162)
at com.amazonaws.http.UrlHttpClient.execute(UrlHttpClient.java:75)
at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:371)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:212)
at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.invoke(AmazonCognitoIdentityClient.java:566)
at com.amazonaws.services.cognitoidentity.AmazonCognitoIdentityClient.getId(AmazonCognitoIdentityClient.java:448)
at com.amazonaws.auth.AWSAbstractCognitoIdentityProvider.getIdentityId(AWSAbstractCognitoIdentityProvider.java:172)
at com.amazonaws.auth.AWSEnhancedCognitoIdentityProvider.refresh(AWSEnhancedCognitoIdentityProvider.java:76)
at com.amazonaws.auth.CognitoCredentialsProvider.startSession(CognitoCredentialsProvider.java:678)
at com.amazonaws.auth.CognitoCredentialsProvider.getCredentials(CognitoCredentialsProvider.java:465)
at com.amazonaws.auth.CognitoCachingCredentialsProvider.getCredentials(CognitoCachingCredentialsProvider.java:485)
at com.amazonaws.auth.CognitoCachingCredentialsProvider.getCredentials(CognitoCachingCredentialsProvider.java:77)
at com.amazonaws.services.lambda.AWSLambdaClient.invoke(AWSLambdaClient.java:406)
at com.amazonaws.services.lambda.AWSLambdaClient.invoke(AWSLambdaClient.java:364)
at com.amazonaws.mobileconnectors.lambdainvoker.LambdaInvocationHandler.invoke(LambdaInvocationHandler.java:62)
at java.lang.reflect.Proxy.invoke(Proxy.java:1006)
at $Proxy1.echo(Unknown Source)
at com.amazonaws.youruserpools.AboutApp$2.onClick(AboutApp.java:80)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/Process: Sending signal. PID: 14152 SIG: 9
Вот мой исходный код:
Button backButton = (Button) findViewById(R.id.aboutBack);
backButton.setOnClickListener(new View.OnClickListener() {
@SuppressLint("StaticFieldLeak")
@Override
public void onClick(View v) {
CognitoCachingCredentialsProvider cognitoProvider = new CognitoCachingCredentialsProvider(
getApplicationContext(), "pool-id", Regions.US_EAST_1);
Log.v("TAG", "ABC " + getApplicationContext().getApplicationContext().toString() + " : " + cognitoProvider.getIdentityProvider());
LambdaInvokerFactory factory = new LambdaInvokerFactory(getApplicationContext().getApplicationContext(),
Regions.US_EAST_1, cognitoProvider);
final MyInterface myInterface = factory.build(MyInterface.class);
Log.v("TAG", "HERE 0 " + factory.getClientContext().toString());
NameInfo nameInfo = new NameInfo("John", "Doe");
Log.v("TAG", "HERE 1 " + nameInfo.getFirstName() + " : " + nameInfo.getLastName());
myInterface.echo(nameInfo);
}
});
MyInterface
import com.amazonaws.mobileconnectors.lambdainvoker.LambdaFunction;
public interface MyInterface {
@LambdaFunction
String echo(NameInfo nameInfo);
@LambdaFunction(functionName = "echo")
void noEcho(NameInfo nameInfo);
}
Любая идея о том, что мне не хватает?