有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

从Android应用程序调用在localhost Tomcat中运行的Servlet时出现java“连接被拒绝”错误

我正试图通过我的Android应用程序访问Servlet方法。 Servlet在localhost上运行,在tomcatv8服务器上运行。 当我使用仿真器时,它工作正常。 但是当我使用真正的设备时,我得到了一个Connection to http://10.0.2.2:8080 refused错误。 安卓设备连接到我的笔记本电脑,Tomcat在那里运行

我可能必须配置tomcat以允许来自同一网络中另一个设备(我的安卓设备)的传入连接?(我不知道怎么做)

Android代码:

HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://10.0.2.2:8080/DistributionLogisticsOptimizer/Login");
        String responseText = "";
        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("email",mEmail));
            nameValuePairs.add(new BasicNameValuePair("password", mPassword));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            /* execute */
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity rp = response.getEntity();
            responseText=readContent(response);
            } 
        catch (ClientProtocolException e) {
                    // TODO Auto-generated catch block
        } 
        catch (IOException e) {
                    e.printStackTrace();
        }
        return responseText;

我还为安卓清单授予了互联网许可

<uses-sdk
安卓:minSdkVersion="14"
安卓:targetSdkVersion="19" />
<uses-permission 安卓:name="安卓.permission.INTERNET"/>

这是Tomcat配置

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

<Engine defaultHost="localhost" name="Catalina">
  <Realm className="org.apache.catalina.realm.LockOutRealm">
    <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
  </Realm>

  <Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true">
    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t &quot;%r&quot; %s %b" prefix="localhost_access_log" suffix=".txt"/>

  <Context docBase="DistributionLogisticsOptimizer" path="/DistributionLogisticsOptimizer" reloadable="true" source="org.eclipse.jst.jee.server:DistributionLogisticsOptimizer"/></Host>
</Engine>

共 (0) 个答案