Jor*_*e T 5 php eclipse rest android
错误:org.apache.http.conn.HttpHostConnectException:http://10.0.2.2:8080拒绝连接当Android在php中使用rest api时,我连接到api
我通过usb使用eclipse和我的Android手机运行我的应用程序.我的api休息是在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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试过不同的方式:
http://10.0.2.2:8080/rest/login
http://localhost:8080/rest/login
http:/127.0.0.1:8080/rest/login
Run Code Online (Sandbox Code Playgroud)
注意:我读过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/ ");
Run Code Online (Sandbox Code Playgroud)
应该:
HttpPost post = new HttpPost ( " http://127.0.0.1:8080/rest/login/ ");
Run Code Online (Sandbox Code Playgroud)
接下来,如果手机未连接到您的计算机网络,它将永远不会连接到您的本地服务器(即计算机)。
如果您家里有 wifi 连接,请检查运行 xampp 的计算机的 IP 地址并将其替换为localhost. 如果没有 wifi 连接并且您的计算机已连接到调制解调器(没有路由器),则只需在此处检查您的 IP 地址并将其替换为localhost.
| 归档时间: |
|
| 查看次数: |
12206 次 |
| 最近记录: |