【发布时间】: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 连接的手机。
但没有任何效果,即使 <uses-permission android:name="android.permission.INTERNET"/> 我在清单中添加了。
抱歉帮帮我!!,PD:我不会说英语
【问题讨论】:
-
http:/127.0.0.1:8080/rest/login/这是错别字吗??