无法连接到esp8266

时间:2019-07-28 15:33:19

标签: java android android-studio

我与设置为接入点的esp 8266连接。使用send方法,我可以将字符串发送到该设备,但有时会出现此错误。

Send方法被调用两次。我使用按钮发送测试字符串,它可以正常工作。我的想法是通过这种方式从edittext发送字符串。

发送(stringFromInput)

但是我得到这个错误。 有什么想法吗?

  

java.net.ConnectException:无法连接到/192.168.4.1:80   2019-07-28 17:28:03.727 5952-5994 / com.example.wifiscan W / System.err:at com.android.okhttp.internal.io.RealConnection.connectSocket(RealConnection.java:143)   2019-07-28 17:28:03.727 5952-5994 / com.example.wifiscan W / System.err:at com.android.okhttp.internal.io.RealConnection.connect(RealConnection.java:112)   2019-07-28 17:28:03.727 5952-5994 / com.example.wifiscan W / System.err:at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:184)   2019-07-28 17:28:03.727 5952-5994 / com.example.wifiscan W / System.err:at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)   2019-07-28 17:28:03.727 5952-5994 / com.example.wifiscan W / System.err:at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)   2019-07-28 17:28:03.727 5952-5994 / com.example.wifiscan W / System.err:at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)   2019-07-28 17:28:03.728 5952-5994 / com.example.wifiscan W / System.err:at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)   2019-07-28 17:28:03.728 5952-5994 / com.example.wifiscan W / System.err:at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)   2019-07-28 17:28:03.728 5952-5994 / com.example.wifiscan W / System.err:at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)   2019-07-28 17:28:03.728 5952-5994 / com.example.wifiscan W / System.err:at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:538)   2019-07-28 17:28:03.728 5952-5994 / com.example.wifiscan W / System.err:at com.example.wifiscan.MainActivity $ TaskEsp.doInBackground(MainActivity.java:232)   2019-07-28 17:28:03.728 5952-5994 / com.example.wifiscan W / System.err:at com.example.wifiscan.MainActivity $ TaskEsp.doInBackground(MainActivity.java:212)   2019-07-28 17:28:03.728 5952-5994 / com.example.wifiscan W / System.err:at android.os.AsyncTask $ 2.call(AsyncTask.java:333)   2019-07-28 17:28:03.728 5952-5994 / com.example.wifiscan W / System.err:at java.util.concurrent.FutureTask.run(FutureTask.java:266)   2019-07-28 17:28:03.728 5952-5994 / com.example.wifiscan W / System.err:at android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:245)   2019-07-28 17:28:03.728 5952-5994 / com.example.wifiscan W / System.err:at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)   2019-07-28 17:28:03.728 5952-5994 / com.example.wifiscan W / System.err:at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:641)   2019-07-28 17:28:03.728 5952-5994 / com.example.wifiscan W / System.err:at java.lang.Thread.run(Thread.java:764)

public void send(String slash){

     ip_= editText.getText().toString();

     serverIP = ip_ + "/" + slash ;
    Log.d("wifitag", serverIP);

    TaskEsp taskEsp = new TaskEsp(serverIP);

    Toast.makeText(this, "Connessione a " + serverIP, Toast.LENGTH_SHORT).show();
    taskEsp.execute();
}


public class TaskEsp extends AsyncTask<Void, Void, String> {

    String server;

    TaskEsp(String server){
        this.server = server;
    }

    @Override
    protected String doInBackground(Void... params) {

        final String p = "http://" + server;


        String serverResponse = "";

        //Using java.net.HttpURLConnection
        try {
            HttpURLConnection httpURLConnection = (HttpURLConnection) (new URL(p).openConnection());

            if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {

                InputStream inputStream = null;
                inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader =
                        new BufferedReader(new InputStreamReader(inputStream));
                serverResponse = bufferedReader.readLine();

                inputStream.close();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            serverResponse = e.getMessage();
            Log.d("Err","Errore 1");
        } catch (IOException e) {
            e.printStackTrace();
            serverResponse = e.getMessage();
            Log.d("Err","Errore 2");

        }

        return serverResponse;
    }


}

0 个答案:

没有答案