微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 错误:org.apache.http.conn.HttpHostConnectException:拒绝连接到http://10.0.2.2:8080

错误:org.apache.http.conn.HttpHostConnectException:连接到http://10.0.2.2:8080拒绝当AndroidPHP中使用rest api时,我连接到api

我通过usb使用eclipse和我的Android手机运行我的应用程序.我的api休息是在PHP中使用并使用xampp,它运行在端口8080 http:// localhost:8080上. Xampp可与我的其他Web应用程序一起使用,但Android不起作用.

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

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

 private class TareaWSInsertar extends AsyncTaskarams) {
            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连接手机.

但没有任何作用,甚至< uses-permission android:name =“android.permission.INTERNET”/>我加入了我的清单.

对不起帮帮我!!,PD:我不会说英语

最佳答案
首先你有一个错误

 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.

原文地址:https://www.jb51.cc/android/431088.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐