gpt4 book ai didi

JAVA与服务器php通信

转载 作者:行者123 更新时间:2023-12-01 19:35:16 26 4
gpt4 key购买 nike

我想写一个非常简单的API。当我使用 postman API 时有效。我想创建 Android 应用程序,但我的应用程序崩溃了。为什么?错误在哪里

OkHttpClient zapytanie = new OkHttpClient();
RequestBody requestBody = new FormBody.Builder()
.add("login", "some_email")
.add("password", "some_password")
.build();
final Request request = new Request.Builder()
.header("Content-Type", "application/x-www-form-urlencoded")
.url("https://garlic-dragon.000webhostapp.com/registration.php")
.post(requestBody)
.build();
try {
Response response = zapytanie.newCall(request).execute();
Toast.makeText(getApplicationContext(), "This is my success!",
Toast.LENGTH_LONG).show();
} catch (IOException e) {
//e.printStackTrace();
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_LONG).show();
}

2019-12-07 19:32:56.007 28055-28055/com.example.chat E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.chat, PID: 28055 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 okhttp3.Dns$Companion$SYSTEM$1.lookup(Dns.kt:48) at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.kt:160) at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.kt:125) at okhttp3.internal.connection.RouteSelector.next(RouteSelector.kt:71) at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:199) at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:109) at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:77) at okhttp3.internal.connection.Transmitter.newExchange$okhttp(Transmitter.kt:162) at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:35) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87) at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:82) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87) at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:84) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112) at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:71) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:112) at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:87) at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.kt:184) at okhttp3.RealCall.execute(RealCall.kt:66) at com.example.a4.MainActivity$2.onClick(MainActivity.java:78) at android.view.View.performClick(View.java:7339) at android.widget.TextView.performClick(TextView.java:14222) at android.view.View.performClickInternal(View.java:7305) at android.view.View.access$3200(View.java:846) at android.view.View$PerformClick.run(View.java:27787) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7078) 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:964)

最佳答案

错误一定是NetworkOnMainThreadException,这是因为您无法在主线程上执行同步HTTP。使用入队不执行异步运行请求。

     OkHttpClient zapytanie = new OkHttpClient();
RequestBody requestBody = new FormBody.Builder()
.add("login", "some_email")
.add("password", "some_password")
.build();
final Request request = new Request.Builder()
.header("Content-Type", "application/x-www-form-urlencoded")
.url("https://garlic-dragon.000webhostapp.com/registration.php")
.post(requestBody)
.build();
try {
Response response = zapytanie.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, Throwable throwable) {
// Something went wrong
}

@Override public void onResponse(Response response) throws IOException {
if (response.isSuccessful()) {
// Do what you want to do with the response.
} else {
// Request not successful
}
}
});
} catch (IOException e) {
//e.printStackTrace();
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_LONG).show();
}

我推荐像 Retrofit 这样的库以便更轻松地调用 API。

关于JAVA与服务器php通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59227869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com