gpt4 book ai didi

java - Android Studio httpURLConnection 应用程序崩溃

转载 作者:太空狗 更新时间:2023-10-29 16:30:23 27 4
gpt4 key购买 nike

我是 Android 和 Java 开发的新手,我是网络开发人员,想学习 Android studio 和 java。

和往常一样,在学习新语言时,我做的第一件事就是调用一些数据库,在这个 JSON 调用中。

使用 httpURLConnection ,我在网上找到了示例,当我在 android studio 中运行它时,它在我的主要功能中工作,但是当我把它放在点击功能的按钮中并在我的手机上运行我的应用程序时,我的应用程序崩溃了。

我也添加了到 list 标签内的 AndroidManifest.xml。

这是我得到的错误

11-22 19:47:46.354 24975-24975/com.example.adis.myapp I/System.out: I am here//////////////////////////////////////////
11-22 19:47:46.494 24975-24975/com.example.adis.myapp D/AndroidRuntime: Shutting down VM
11-22 19:47:46.494 24975-24975/com.example.adis.myapp W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41659e00)
11-22 19:47:46.514 24975-24975/com.example.adis.myapp E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.example.adis.myapp, PID: 24975
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
ive Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4457) 
at android.view.View$PerformClick.run(View.java:18491) 
at android.os.Handler.handleCallback(Handler.java:733) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5333) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:711) 
at dalvik.system.NativeStart.main(Native Method) 
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1148)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:503)
at com.example.adis.myapp.MainActivity.loginFunction(MainActivity.java:85)
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:4457) 
at android.view.View$PerformClick.run(View.java:18491) 
at android.os.Handler.handleCallback(Handler.java:733) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:136) 
at android.app.ActivityThread.main(ActivityThread.java:5333) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:711) 
at dalvik.system.NativeStart.main(Native Method) 
11-22 19:47:46.524 923-1275/? W/ActivityManager: Force finishing activity com.example.adis.myapp/.MainActivity

这是我的 onClick 函数

public void loginFunction(View view) throws IOException, JSONException {


System.out.println("I am here//////////////////////////////////////////");
try {

URL url = new URL("http://echo.jsontest.com/insert-key-here/insert-value-here/key/value");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");

if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}

BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));

String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}

conn.disconnect();

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}



}

如果你看到一些即使是 Java 初学者也应该看到的愚蠢错误,请对我严厉一些,因为我没有看到。

感谢您的光临:)

最佳答案

您不能在主线程上建立网络连接,因为这会卡住 UI 线程并使您的应用停止响应。这就是抛出异常的原因。

您可以使按钮触发新线程或 AsyncTask 以完成操作。

例子: How to fix android.os.NetworkOnMainThreadException?

关于java - Android Studio httpURLConnection 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40749664/

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