将数据从Android发送到ESP8266
我有一个Arduino项目,可以在其中设置一个ESP8266作为Web服务器,并且可以将数据发送到该网络服务器。如果我将“ http://192.168.4.1/get?data = 010”放入浏览器中,它可以很好地工作。
我想使用Android应用程序发送数据,这几乎意味着使用上述URL,而“数据”的值不同。 我已经尝试使用OKHTTP3,但它不起作用。
这是我尝试过的:
public void sendMessage(View view) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://192.168.4.1/get?data=010")
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
public void onResponse(Call call, Response response)
throws IOException {
System.out.println(response.toString());
}
public void onFailure(Call call, IOException e) {
System.out.println("Failed");
}
});
}
这似乎与其他API一起起作用,例如。如果我放入 https://reqres.in/api/api/users?page=2 作为URL,我会得到一个响应,但是当我尝试连接到Arduino时,它无能为力。
I have an arduino project, where i was able to set up an esp8266 as a webserver, and i am able to send data to it, eg. if i put "http://192.168.4.1/get?data=010" into the browser, it works perfectly.
I want to send data using an android app, which pretty much means using the above mentioned url, just with different values for "data".
I've tried using okhttp3, but it doesn't work.
Here is what I've tried:
public void sendMessage(View view) throws IOException {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://192.168.4.1/get?data=010")
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
public void onResponse(Call call, Response response)
throws IOException {
System.out.println(response.toString());
}
public void onFailure(Call call, IOException e) {
System.out.println("Failed");
}
});
}
This seems to work, with other apis, eg. if i put in https://reqres.in/api/users?page=2 as the url I get a response, but when i try to connect to the arduino, it doesn't do anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加
Android:usescleartexttraffic =“ true”
添加到androidmanifest.xml文件解决了问题。感谢您的答案 blackapps 。
Adding
android:usesClearTextTraffic="true"
to the AndroidManifest.xml file solved the issue.Thanks for the answer blackapps.