【发布时间】:2015-06-15 19:11:19
【问题描述】:
我正在尝试使用 android studio 通过 android 模拟器连接到 xampp localhost。但我总是收到连接被拒绝的错误。
我已经阅读并尝试了大多数解决方案,例如将 localhost 更改为 10.0.2.2 或 127.0.0.1 或 192.168.0.1,甚至尝试删除 http:// 或在其后面添加端口 :8080。但仍然没有运气。
我可以在浏览器中运行 localhost/test.php 没有问题。但是 10.0.2.2/test.php 不起作用。
当然,我也向 manifest.xml 添加了 Internet 权限。
我什至尝试了 2 个版本的代码,但它仍然失败了。谁能帮我理解这个?
这是显示的错误。
04-10 02:55:15.933 2162-2178/com.example.kitd16.myapplication W/System.err﹕ java.net.ConnectException: failed to connect to /10.0.0.2 (port 80) after 60000ms: isConnected failed: ECONNREFUSED (Connection refused)
这是我尝试过的代码。
URL url = new URL("http://127.0.0.1/cloudtest/test.php");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(30000 /* milliseconds */);
conn.setConnectTimeout(60000 /* milliseconds */);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
conn.setRequestProperty("Content-Length", "" + et.getText().toString());
conn.setUseCaches(false);
conn.setInstanceFollowRedirects(false);
conn.setDoInput(true);
//Send request
DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
wr.writeBytes(et.getText().toString());
wr.flush();
wr.close();
// Starts the query
conn.connect();
is = (BufferedInputStream) conn.getInputStream();
} catch (Exception ex) {
System.out.println("first error");
ex.printStackTrace();
System.exit(0);
} finally {
if (is != null) {
try {
is.close();
} catch (Exception ex) {
}
}
}
第二版
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final ProgressDialog p = new ProgressDialog(v.getContext()).show(v.getContext(),"Waiting for Server", "Accessing Server");
Thread thread = new Thread()
{
@Override
public void run() {
try{
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://10.0.0.2/my_folder_inside_htdocs/connection.php"); // make sure the url is correct.
//add your data
nameValuePairs = new ArrayList<NameValuePair>(1);
// Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
nameValuePairs.add(new BasicNameValuePair("Edittext_value",et.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value'];
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response);
runOnUiThread(new Runnable() {
public void run() {
p.dismiss();
tv.setText("Response from PHP : " + response);
}
});
}catch(Exception e){
runOnUiThread(new Runnable() {
public void run() {
p.dismiss();
}
});
System.out.println("Exception : " + e.getMessage());
}
}
};
thread.start();
【问题讨论】:
-
127.0.0.1 不适用于模拟器尝试 10.0.0.2,请将您的日志输入您的问题,您可以使用浏览器访问相同的 php 文件
-
我认为您在清单文件中添加了 Internet 权限
-
尝试了 10.0.0.2,是的,我已经添加了它。还是不行。我只是不断收到同样的错误。
-
你试过了吗:stackoverflow.com/questions/20015883/… 使用你的系统 ip 而不是 10.0.0.2 或 127.0.0.1
-
尝试将任何 index.html 放在 xampp 主目录中,并在模拟器浏览器中测试 10.0.2.2,如果可行,它也适用于其他文件。
标签: android connection xampp localhost