gpt4 book ai didi

php - 错误 :org. apache.http.conn.HttpHostConnectException : Connection to http://10. 0.2.2:8080 被拒绝

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:45:15 24 4
gpt4 key购买 nike

错误:org.apache.http.conn.HttpHostConnectException:连接到http://10.0.2.2:8080拒绝当 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 连接的手机。

但没有任何效果,即使是<uses-permission android:name="android.permission.INTERNET"/>我在我的 list 中添加了。

抱歉帮帮我!!,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

关于php - 错误 :org. apache.http.conn.HttpHostConnectException : Connection to http://10. 0.2.2:8080 被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23987635/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com