0

我正在使用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();
                }
        }
    });

}


    }  
4

1 回答 1

2

1)我认为,您已经将此权限添加到清单文件中。

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

2) 活动

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

3) 在 button.onclick 调用 doTest2() 方法。我将 Apache HttpClient 更改为 java.net.HttpURLConnection。

请看这个链接

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

我希望这可以帮助你。

于 2018-04-02T20:12:11.440 回答