【问题标题】:Error:org.apache.http.conn.HttpHostConnectException: Connection to http://10.0.2.2:8080 refused错误:org.apache.http.conn.HttpHostConnectException:连接到 http://10.0.2.2:8080 被拒绝
【发布时间】:2014-06-02 05:05:57
【问题描述】:

Error:org.apache.http.conn.HttpHostConnectException: Connection to http://10.0.2.2:8080 denied 当Android在php中使用rest api,我连接到api时

我通过 usb 使用 eclipse 和我的 android 手机来运行我的应用程序。我的 api rest 是用 php 制作的并使用 xampp,它在端口 8080 http://localhost:8080 上运行。 Xampp 可以与我的其他 Web 应用程序一起使用,但 Android 无法使用。

-另外,我的手机和我的电脑没有连接到同一个网络。 -rest api 在这种情况下可以在没有互联网的情况下工作吗?

我在 android 中的代码是这样的:

 private class TareaWSInsertar extends AsyncTask<String,Integer,Boolean> {
        //agregado user_id, user_fullname, user_email
        private int user_id;
        private String user_fullname;
        private  String user_email;

        protected Boolean doInBackground(String... params) {
            boolean resul = true;

            HttpClient httpClient = new DefaultHttpClient();


            HttpPost post = new HttpPost("http://10.0.2.2:8080/rest/login/");
        post.setHeader("content-type", "application/json");

        try
            {
            //Construimos el objeto cliente en formato JSON
            JSONObject dato = new JSONObject();


            dato.put("email", params[0]);
            dato.put("pwd", params[1]);

            StringEntity entity = new StringEntity(dato.toString());
            post.setEntity(entity);

                HttpResponse resp = httpClient.execute(post);
                String respStr = EntityUtils.toString(resp.getEntity());

                JSONObject respJSON = new JSONObject(respStr);

                user_id = respJSON.getInt("user_id");
                user_fullname = respJSON.getString(user_fullname);
                user_email = respJSON.getString("user_email");


                if(!respStr.equals("true"))
                    resul = false;
            }
            catch(Exception ex)
            {
                Log.e("ServicioRest","Error!", ex);
                resul = false;
            }

            return resul;
        }

            protected void onPostExecute(Boolean result) {

            if (result)
            {
             Toast.makeText(getApplicationContext(), "" + user_id+ "-" +    user_fullname + "-" + user_email, Toast.LENGTH_LONG).show();
            }
        }
    }

我尝试了不同的方法:

http://10.0.2.2:8080/rest/login
 http://localhost:8080/rest/login
 http:/127.0.0.1:8080/rest/login

注意:我读到http://10.0.2.2 仅在使用 android 模拟器时有效, 我使用通过 USB 连接的手机。

但没有任何效果,即使 &lt;uses-permission android:name="android.permission.INTERNET"/&gt; 我在清单中添加了。

抱歉帮帮我!!,PD:我不会说英语

【问题讨论】:

  • http:/127.0.0.1:8080/rest/login/ 这是错别字吗??

标签: php android eclipse rest


【解决方案1】:

首先你有一个错误:

 HttpPost post = new HttpPost ( " http:/127.0.0.1:8080/rest/login/ "); 

应该是:

HttpPost post = new HttpPost ( " http://127.0.0.1:8080/rest/login/ ");

接下来,如果手机未连接到您的计算机网络,它将永远不会连接到您的本地服务器,也就是您的计算机。

如果您家中有 wifi 连接,请检查运行 xampp 的计算机的 IP 地址,并将其替换为 localhost。如果没有 wifi 连接并且您的计算机连接到调制解调器(没有路由器),则只需检查您的 IP 地址here 并将其替换为localhost

【讨论】:

  • 这是一个不同的问题:)。请接受这个并要求另一个,我很乐意提供帮助
  • @slowFly ,请打开一个新问题。然后在评论中添加链接。我很乐意帮助你。
【解决方案2】:

我设法通过将我的 IP 地址从 cmd -> ipconfig 写入 url 来解决这个问题:

HttpPost post = new HttpPost("http://192.168.x.x/rest/login/");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    相关资源
    最近更新 更多