我正在使用IP将数据从android传输到arduino,希望我能够通过在设置中的wifi列表中选择其名称来建立与arduino + esp8266 wifi模块的连接,因为它作为接入点工作。此外,通过任何浏览器,我只需编写“192.168.4.1:80?pin=13”即可将数据发送到 IP。但是我对android有一个问题,因为arduino没有收到它,所以它没有传输请求。
这是我的代码,我还在 android manifest 中包含了互联网权限。它有什么问题?
final Button image=(Button) findViewById(R.id.button1);
image.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
image.setText("making change");
String urlGET = "http://192.168.4.1:80/?pin=13";
HttpGet getMethod = new HttpGet(urlGET);
// if you are having some headers in your URL uncomment below line
//getMethod.addHeader("Content-Type", "application/form-data");
HttpResponse response = null;
HttpClient httpClient = new DefaultHttpClient();
try {
response = httpClient.execute(getMethod);
int responseCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
String responseBody = null;
if (entity != null) {
responseBody = EntityUtils.toString(entity);
//here you get response returned from your server
Log.e("response = ", responseBody);
// response.getEntity().consumeContent();
}
JSONObject jsonObject = new JSONObject(responseBody);
// do whatever you want to do with your json reponse data
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
}
}