我正在尝试通过 phone 上的 android activity 与笔记本电脑上的 servlet 进行交互

I am trying to interact with a servlet on my laptop through an android activity on my phone

我正在尝试从正在 phone(USB) 上测试的 android activity 与笔记本电脑上 Apache Tomcat7 中的 Servlet 进行交互。我正在创建一个按钮。单击我应该转到下一个 "activity"。但是,当我尝试在两者之间联系服务器时,它会停止响应并且不会转到下一页。如果需要此信息,我正在 Ubuntu 作为我的笔记本电脑 OS 和 OnePlus 作为我的 phone。

以下是 Android activity 的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_intent);

    Button submit = (Button) findViewById(R.id.button1);

    submit.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {

            new Login1().execute();
        }
    });
}

private class Login1 extends AsyncTask<String, String, String> {
    @Override
    protected String doInBackground(String... paramss) {

        DefaultHttpClient httpClient = new DefaultHttpClient();
        String url = "http://192.168.1.100:8080/Server1/Server";

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("u", "username"));
        params.add(new BasicNameValuePair("p", "pass"));

        HttpPost httpPost = new HttpPost(url);
       Log.i("httpPost","success");
       try {

           httpPost.setEntity(new UrlEncodedFormEntity(params));
           Log.i("setEntity", "success");
           HttpResponse httpResponse = httpClient.execute(httpPost);
           Log.i("execute", "success");

           Intent login = new Intent(getApplicationContext(), act2.class);
           startActivity(login);

        } catch (ClientProtocolException e) {
           e.printStackTrace();
           Log.i("Exception","ClientProtocolException");
       } catch (UnsupportedEncodingException e) {
           e.printStackTrace();
           Log.i("Exception", "UnsupportedEncodingException");
       } catch (IOException e) {
           e.printStackTrace();
           Log.i("Exception", "IOException");
       }

        return null;
    }
}

以下是我在 servlet 上的 doPost 方法。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String name=request.getParameter("u").toString();  
        String password=request.getParameter("p").toString();
        response.getWriter().write(name+password); //working
    }

这是我尝试过的方法:我在笔记本电脑上创建了一个本地 java 文件。试图与apache tomcat7联系。它可以联系并获得所需的结果。但是我的 android activity 无法与服务器联系。它不会将控制传递给 act2 Activity.

我正处于学习阶段,所以我尽量让事情变得简单。

附加信息:如果需要,我已连接到 wifi 互联网、家庭网络。

logcat 输出:

> 05-22 04:42:16.368  21511-23633/com.example.bhavya.sampleintent I/httpPost﹕ success
05-22 04:42:16.369  21511-23633/com.example.bhavya.sampleintent I/setEntity﹕ success
05-22 04:43:19.575  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.1.100:8080 refused
05-22 04:43:19.576  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
05-22 04:43:19.576  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-22 04:43:19.576  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-22 04:43:19.576  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-22 04:43:19.576  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at com.example.bhavya.sampleintent.IntentAct$Login1.doInBackground(IntentAct.java:117)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at com.example.bhavya.sampleintent.IntentAct$Login1.doInBackground(IntentAct.java:99)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at android.os.AsyncTask.call(AsyncTask.java:288)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at java.lang.Thread.run(Thread.java:818)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ Caused by: java.net.ConnectException: failed to connect to /192.168.1.100 (port 8080): connect failed: ETIMEDOUT (Connection timed out)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:124)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
05-22 04:43:19.577  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
05-22 04:43:19.578  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at java.net.Socket.connect(Socket.java:882)
05-22 04:43:19.578  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
05-22 04:43:19.578  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
05-22 04:43:19.578  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ ... 14 more
05-22 04:43:19.578  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ Caused by: android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
05-22 04:43:19.578  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at libcore.io.Posix.connect(Native Method)
05-22 04:43:19.578  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
05-22 04:43:19.578  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
05-22 04:43:19.578  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.java:122)
05-22 04:43:19.578  21511-23633/com.example.bhavya.sampleintent W/System.err﹕ ... 19 more
05-22 04:43:19.578  21511-23633/com.example.bhavya.sampleintent I/Exception﹕ IOException

我尝试使用本地移动热点网络,将其与互联网断开连接。但是没用。

注意:我没有在模拟器上工作。我正在研究 USB 调试。

因此,您可以通过您的机器访问本地主机,但无法从 android 设备访问本地主机。

这意味着您的 android 设备使用 2G/3G 或 WIFI 连接访问本地主机。如果你当时使用localhost连接,你必须在同一个网络中,那么只有你可以访问localhost。

但是在模拟器中你在同一个网络(可能是本地主机网络)中,这就是它工作的原因。

最后,您的方法可能的解决方案是

  1. 在您的 android 设备中使用相同的网络(本地主机网络) 还。意味着通过 Wifi 连接到同一本地主机互联网 联系。这次 2G/3G 连接将无法工作,因为它们不是 在同一网络中
  2. 在外部部署构建并访问外部 ip 连接地址。这次2G/3G或WIFI都能用