Java客户端Android在虚拟设备上工作,在真实设备上没有

时间:2018-02-04 14:15:40

标签: java android client

在虚拟设备上工作,在实际设备上没有。检查了所有ip,很多端口,但没有。

Android 7.1.1 Lenovopad TB-X304F

在清单中:

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

代码:

public class MainActivity extends AppCompatActivity {

EditText e1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    e1 = (EditText)findViewById(R.id.editText);
}

public void send_text(View v) {
    String message = e1.getText().toString();
    myTask mt = new myTask();
    mt.execute(message);

    Toast.makeText(getApplicationContext(),"Data sent: "+e1.getText().toString(), Toast.LENGTH_LONG).show();
}

class myTask extends AsyncTask<String, Void, Void>{
    Socket s;
    PrintWriter printWriter;

    @Override
    protected Void doInBackground(String... params){
        try {
            String message = params[0];
            s = new Socket("192.168.1.5", 6001);
            printWriter = new PrintWriter(s.getOutputStream());
            printWriter.write(message);
            printWriter.flush();
            printWriter.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

错误:

W / System.err:java.net.ConnectException:连接超时

1 个答案:

答案 0 :(得分:0)

错误是我不知道我必须禁用防火墙并打开端口。我打开了端口,一切正常。