Android 到 XAMPP 的连接被拒绝
Posted
技术标签:
【中文标题】Android 到 XAMPP 的连接被拒绝【英文标题】:Android to XAMPP Connection Refused 【发布时间】: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,是的,我已经添加了它。还是不行。我只是不断收到同样的错误。 你试过了吗:***.com/questions/20015883/… 使用你的系统 ip 而不是 10.0.0.2 或 127.0.0.1 尝试将任何 index.html 放在 xampp 主目录中,并在模拟器浏览器中测试 10.0.2.2,如果可行,它也适用于其他文件。 【参考方案1】:对我来说,使用我自己的 PC IP 地址与 Android Studio 模拟器一起工作。单击按钮后-
HttpPost httpPost=new HttpPost("http://192.168.104.222/index.php");
我在onCreate
方法中使用了严格模式策略:
protected void onCreate(Bundle savedInstanceState)
//Strict Mode Policy
StrictMode.ThreadPolicy policy =new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_login);
eName=(EditText)findViewById(R.id.etName);
public void Login(View v)
InputStream is=null;
String name=eName.getText().toString();
List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("name",name));
try
//Setting up the default http client
HttpClient httpClient =new DefaultHttpClient();
//Setting up the http post method & passing the url in case
//of online database and the IP address in case of localhost database
//And the php file which serves as the link between the android app
//and the database.
//Second parameter is the php file to link with database
HttpPost httpPost=new HttpPost("http://192.168.104.222/tutorial.php");
//Passing the nameValue pairs inside the httpPOST
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Getting the response
HttpResponse response =httpClient.execute(httpPost);
HttpEntity entity =response.getEntity();
is=entity.getContent();
//confirmation
String msg ="Data Entered Successfully";
catch(ClientProtocolException e)
Log.e("ClientProtocol","Log_TAG");
e.printStackTrace();
catch(IOException e)
// Log.e("IO Error","Log_TAG");
e.printStackTrace();
【讨论】:
以上是关于Android 到 XAMPP 的连接被拒绝的主要内容,如果未能解决你的问题,请参考以下文章
将 XAMPP SQL 数据库添加到 PhpStorm 会导致被拒绝的详细信息错误