gpt4 book ai didi

Java Android - 在不打开浏览器的情况下按下按钮时调用 URL

转载 作者:行者123 更新时间:2023-11-29 04:49:16 24 4
gpt4 key购买 nike

我想制作一个简单的 Android 应用程序,其中包含调用 Arduino 设备上的 URL 以打开和关闭灯的按钮。不需要打开网络浏览器。

我是 Android 的新手,我已经在这里搜索并找到了一些建议,但它们对我不起作用。

也许有人可以指引我正确的方向?

到目前为止,这是我的代码,当我按下按钮时,没有任何反应。

package de.triscus.arduinoweb;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;


import java.io.IOException;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class HomeLight extends AppCompatActivity implements OnClickListener {
String msg = "Android : ";
private Button lichterkette1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_light);

lichterkette1 = (Button) findViewById(R.id.Lichterkette1);
lichterkette1.setOnClickListener(this);
}

public void onClick(View v) {
URL url = null;
HttpURLConnection urlConnection = null;
switch (v.getId())

{
case R.id.Lichterkette1:

try {

url = new URL("http://192.168.2.106/?Lichterkette=1");
urlConnection = (HttpURLConnection) url.openConnection();
// urlConnection = (HttpURLConnection) url.openConnection();
Log.d(msg, "Lichterkette1 pressed");
//InputStream in = new BufferedInputStream(urlConnection.getInputStream());
// Log.d(msg, InputStream);

} catch (MalformedURLException e) {
e.printStackTrace();
Log.d(msg, "URL Malformed");
} catch (IOException e) {
e.printStackTrace();
Log.d(msg, "IO exception");
} finally {
urlConnection.disconnect();
Log.d(msg, "Disconnected");
}

}


}


}

这是 logcat 输出:

03-16 15:19:26.133 9805-9805/? I/art: Late-enabling -Xcheck:jni
03-16 15:19:26.143 9805-9805/? I/art: VMHOOK: rlim_cur : 0 pid:9805
03-16 15:19:26.173 9805-9815/? I/art: Debugger is no longer active
03-16 15:19:26.193 9805-9805/? E/Typeface: SANS_LOC file not found.
03-16 15:19:26.584 9805-9805/? D/Atlas: Validating map...
03-16 15:19:26.684 9805-9835/? I/Adreno-EGL: <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.AF.1.1_RB1.05.00.02.006.020 - CR771817 ()
OpenGL ES Shader Compiler Version: E031.25.03.06
Build Date: 03/04/15 Wed
Local Branch:
Remote Branch: refs/tags/AU_LINUX_ANDROID_LA.AF.1.1_RB1.05.00.02.006.020
Local Patches: NONE
Reconstruct Branch: NOTHING
03-16 15:19:33.481 9805-9805/de.triscus.arduinoweb D/Android :: Lichterkette1 pressed
03-16 15:19:33.481 9805-9805/de.triscus.arduinoweb D/Android :: Disconnected
03-16 15:19:34.832 9805-9805/de.triscus.arduinoweb D/Android :: Lichterkette1 pressed
03-16 15:19:34.832 9805-9805/de.triscus.arduinoweb D/Android :: Disconnected

提前致谢

三角肌

附言:允许使用互联网/网络

最佳答案

网络操作/调用无法在主线程中完成。您需要从另一个线程或异步任务或 Intent 服务运行它

注意:所有 UI 操作都应该在 PostExecute、onPreExecute 上完成

下面的代码可以帮助你解决。

public void onClick(View v) {
switch (v.getId())
{
new Lichterkette().execute();
}
}


class Lichterkette extends AsyncTask<String,Void,String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected String doInBackground(String... params) {
StringBuilder sb=null;
BufferedReader reader=null;
String serverResponse=null;
try {

URL url = new URL("http://192.168.2.106/?Lichterkette=1");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
connection.connect();
int statusCode = connection.getResponseCode();
//Log.e("statusCode", "" + statusCode);
if (statusCode == 200) {
sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
}

connection.disconnect();
if (sb!=null)
serverResponse=sb.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return serverResponse;
}

@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//All your UI operation can be performed here
System.out.println(s);
}
}

关于Java Android - 在不打开浏览器的情况下按下按钮时调用 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36038753/

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