gpt4 book ai didi

java - 我只想通过android发送get请求,但它不起作用,我的代码如下,

转载 作者:行者123 更新时间:2023-12-01 06:22:00 24 4
gpt4 key购买 nike

  public void sendGET(String GET_URL) throws IOException {
try {
URL obj = new URL(GET_URL);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
}
catch (IOException e) {
e.printStackTrace();
}
}


这是我为获取请求编写的方法。
在我的情况下,GET_URL是一个字符串,其值GET_URL = 192.168.4.1?value = 1。
我在按钮单击时调用了此方法,但在android中却无法正常工作。
注意:我的apk编译时没有任何错误,它也可以运行,但是get请求不起作用。
按钮代码如下,

    led1on.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
sendGET("http://192.168.4.1/?linker=1");
Toast.makeText(getApplicationContext(),
"Led1 ON Clicked!", Toast.LENGTH_SHORT).show();
}
catch (IOException e){

}
}
});


崩溃日志在这里:
07-29 16:10:07.352 3459-3459 / com.example.samiullah.esp8266 E / AndroidRuntime:FATAL EXCEPTION:main
                                                                             进程:com.example.samiullah.esp8266,PID:3459
                                                                             android.os.NetworkOnMainThreadException
                                                                                 在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273)
                                                                                 在libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110)
                                                                                 在libcore.io.IoBridge.connectErrno(IoBridge.java:137)
                                                                                 在libcore.io.IoBridge.connect(IoBridge.java:122)
                                                                                 在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
                                                                                 在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:452)
                                                                                 在java.net.Socket.connect(Socket.java:884)
                                                                                 在com.android.okhttp.internal.Platform.connectSocket(Platform.java:117)
                                                                                 在com.android.okhttp.internal.http.SocketConnector.connectRawSocket(SocketConnector.java:160)
                                                                                 在com.android.okhttp.internal.http.SocketConnector.connectCleartext(SocketConnector.java:67)
                                                                                 在com.android.okhttp.Connection.connect(Connection.java:152)
                                                                                 在com.android.okhttp.Connection.connectAndSetOwner(Connection.java:185)
                                                                                 在com.android.okhttp.OkHttpClient $ 1.connectAndSetOwner(OkHttpClient.java:128)
                                                                                 在com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:341)
                                                                                 在com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330)
                                                                                 在com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248)
                                                                                 在com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433)
                                                                                 在com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384)
                                                                                 在com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497)
                                                                                 在com.example.samiullah.esp8266.MainActivity.sendGET(MainActivity.java:110)
                                                                                 在com.example.samiullah.esp8266.MainActivity $ 1.onClick(MainActivity.java:29)
                                                                                 在android.view.View.performClick(View.java:5198)
                                                                                 在android.view.View $ PerformClick.run(View.java:21147)
                                                                                 在android.os.Handler.handleCallback(Handler.java:739)
                                                                                 在android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                 在android.os.Looper.loop(Looper.java:148)
                                                                                 在android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                 在java.lang.reflect.Method.invoke(本机方法)
                                                                                 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-29 16:10:09.956 3459-3459 / com.example.samiullah.esp8266 I / Process:正在发送信号。 PID:3459 SIG:9

最佳答案

在您的AndroidMenifest.xml中,添加

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


我看到了您的日志,对于不熟悉android的人来说这是一个常见问题。尝试

new Thread(() -> sendGET("http://192.168.4.1/?linker=1")).start();


这不是一个好方法,但是可以解决您的问题。看看 How do I fix android.os.NetworkOnMainThreadException?

关于java - 我只想通过android发送get请求,但它不起作用,我的代码如下,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51578979/

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