Android到XAMPP连接被拒绝

时间:2015-04-10 06:49:28

标签: android connection xampp localhost

我正在尝试使用android studio连接到xampp localhost通过android模拟器。但我总是得到连接拒绝错误。

我已经阅读并尝试了大多数解决方案,例如将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的互联网权限。

我甚至尝试了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();

1 个答案:

答案 0 :(得分:0)

对我来说,使用自己的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();

    }


}