Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用get by IP通过wifi将数据从android手机发送到arduino+;esp8266_Android_Arduino_Esp8266 - Fatal编程技术网

使用get by IP通过wifi将数据从android手机发送到arduino+;esp8266

使用get by IP通过wifi将数据从android手机发送到arduino+;esp8266,android,arduino,esp8266,Android,Arduino,Esp8266,我正在使用IP将数据从android传输到arduino,希望我能够通过在设置的wifi列表中选择arduino+esp8266 wifi模块的名称来建立与该模块的连接,因为该模块作为接入点工作。同样,通过任何浏览器,我只需写下“192.168.4.1:80?pin=13”就可以向IP发送数据。但是我有一个android的问题,那就是get没有发送请求,因为arduino没有接收到它 这是我的代码,我还在android清单中包含了互联网权限。怎么了 final Button image=(

我正在使用IP将数据从android传输到arduino,希望我能够通过在设置的wifi列表中选择arduino+esp8266 wifi模块的名称来建立与该模块的连接,因为该模块作为接入点工作。同样,通过任何浏览器,我只需写下“192.168.4.1:80?pin=13”就可以向IP发送数据。但是我有一个android的问题,那就是get没有发送请求,因为arduino没有接收到它

这是我的代码,我还在android清单中包含了互联网权限。怎么了

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();
                }
        }
    });

}


    }  
1) 我想,您已经将此权限添加到清单文件

  <uses-permission android:name="android.permission.INTERNET"/>
3) 在button.onclick处调用doTest2()方法。 我将Apache HttpClient更改为java.net.HttpURLConnection


我希望这对您有所帮助。

您应该在代码中粘贴不带制表符的内容(用4个空格替换)。然后在整个块上使用Ctrl+K将其缩进四个空格以获得代码设置。
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // ...
    // if you perform a networking operation in the main thread
    // you must add these lines.
    // OR (I prefer) you can do asynchronously.

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);
}
private void doTest2() {
    String urlGET = "http://192.168.4.1:80/?pin=13";
    URL url;
    HttpURLConnection urlConnection = null;
    try {
        url = new URL(urlGET);
        urlConnection = (HttpURLConnection) url.openConnection();

        InputStream in = urlConnection.getInputStream();
        InputStreamReader isr = new InputStreamReader(in);

        StringBuffer sb = new StringBuffer();
        int data = isr.read();
        while (data != -1) {
            char current = (char) data;
            sb.append(current);
            data = isr.read();
        }

        System.out.print(sb.toString());
        JSONObject jsonObject = new JSONObject(sb.toString());

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}