0

IPを使用してAndroidからarduinoにデータを転送しています。うまくいけば、アクセスポイントとして機能するため、設定のwifiリストでその名前を選択することにより、arduino + esp8266 wifiモジュールとの接続を確立できます。また、どのブラウザからでも、「192.168.4.1:80?pin=13」と書き込むだけで IP にデータを送信できます。ただし、arduinoが受信していないため、リクエストを送信しないAndroidに問題があります。

これが私のコードです。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();
                }
        }
    });

}


    }  
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 に答える