簡體   English   中英

無法使用 android studio 連接到“xampp server localhost”

[英]Can not connect to "xampp server localhost" with android studio

我嘗試使用 AsyncTask 類通過 android studio 連接到我的本地主機 xampp。 這是 AsyncTask 但它給出錯誤提示Failed to connect to /10.0.2.2:800

我嘗試了我的真實 IP 地址 cmd --> ipconfig IPV4 和其他一些技巧,但沒有成功

下面是我的應用程序結構

使用類的 AsyncTask

package app.buil.land.food.doymaj.doymaj;

import android.app.AlertDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;

/**
 * Created by ProgrammingKnowledge on 1/5/2016.
 */
public class backGroundActivities extends AsyncTask<String,Void,String> {
    Context context;
    AlertDialog alertDialog;
    backGroundActivities (Context ctx) {
        context = ctx;
    }
    @Override
    protected String doInBackground(String... params) {
        String type = params[0];
        String login_url = "http://10.0.2.2:800/login.php";
        if(type.equals("login")) {
            try {
                String phone_number = params[1];
//                String password = params[2];
                URL url = new URL(login_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                OutputStream outputStream = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String post_data = URLEncoder.encode("phone_number","UTF-8")+"="+URLEncoder.encode(phone_number,"UTF-8");
                bufferedWriter.write(post_data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();
                InputStream inputStream = httpURLConnection.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
                String result="";
                String line="";
                while((line = bufferedReader.readLine())!= null) {
                    result += line;
                    Log.e("Line_from_php_server",result);
                }
                bufferedReader.close();
                inputStream.close();
                httpURLConnection.disconnect();
                return result;
            } catch (MalformedURLException e) {
                e.printStackTrace();


            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPreExecute() {
        alertDialog = new AlertDialog.Builder(context).create();
        alertDialog.setTitle("Login Status");
    }

    @Override
    protected void onPostExecute(String result) {
        alertDialog.setMessage(result);
        alertDialog.show();
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }
}

這是我的主要活動

package app.buil.land.food.doymaj.doymaj;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    EditText phone_Number;
    EditText user_Name;
    EditText pass_Word;
    Button singButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


     //   singButton = (Button)findViewById(R.id.sing_id);
     //   singButton.setOnClickListener(kyListener);
        phone_Number =(EditText)findViewById(R.id.mobile_id);
        user_Name =(EditText)findViewById(R.id.name_id);
        pass_Word =(EditText)findViewById(R.id.p_id);




    }

//    Push_Identity db = new Push_Identity(this,"CustomerDatabas.db",null,1);


    public boolean validate(EditText[] fields){
        for(int i = 0; i < fields.length; i++){
            EditText currentField = fields[i];

            if(currentField.getText().toString().length() <= 0){

                switch (i){

                    case 0:

                        Toast.makeText(getApplicationContext(),"شماره تلفن وارد نشده  ",Toast.LENGTH_LONG).show() ;
                        break;



                    case 1:

                        Toast.makeText(getApplicationContext(),"نام کاربری وارد نشده  ",Toast.LENGTH_LONG).show() ;
                        break;


                    case 2:

                        Toast.makeText(getApplicationContext(),"رمز وارد نشده  ",Toast.LENGTH_LONG).show() ;
                        break;

                }
                return false;
            }
        }
        return true;
    }



    //private View.OnClickListener kyListener = new View.OnClickListener() {
        public void send_number(View v) {
            // do something when the button is clicked
            // Yes we will handle click here but which button clicked??? We don't know


            boolean fieldsOK = validate(new EditText[] { phone_Number, user_Name, pass_Word });



                 if (fieldsOK ==true){

                          String Phon = phone_Number.getText().toString();
                          Toast.makeText(getApplicationContext(),Phon,Toast.LENGTH_LONG);
                          // the Tooast dose not display anything
                       //    it seems that the EditText is empty buy i enter the value in
                        // in it.
                          String type ="login";

                          backGroundActivities back = new backGroundActivities(this);
                          back.execute(type,Phon);
        //
                 }
        }
   // };



}

XML

        <EditText
            android:id="@+id/mobile_id"
            android:layout_width="match_parent"
            android:layout_height="43dp"
            android:background="@drawable/customized_edtitext"
            android:drawablePadding="15dp"
            android:paddingRight="10dp"
            android:paddingLeft="15dp"
            android:drawableRight="@drawable/ic_smartphone"
            android:elevation="2dp"
            android:textStyle="bold"
            android:hint="@string/register_phone_commnet"
            android:fontFamily="@font/iransansmedium"
            android:textSize="12sp"


            />


        <EditText
            android:id="@+id/name_id"
            android:layout_width="match_parent"
            android:layout_height="43dp"
            android:layout_below="@id/mobile_id"
            android:background="@drawable/customized_edtitext1"
            android:drawablePadding="15dp"
            android:paddingRight="10dp"
            android:paddingLeft="15dp"
            android:drawableRight="@mipmap/ic_user"
            android:elevation="2dp"
            android:textStyle="bold"
            android:hint="@string/register_user_commnet"
            android:fontFamily="@font/iransansmedium"
            android:textSize="12dp" />

        <EditText
            android:id="@+id/p_id"
            android:layout_width="match_parent"
            android:layout_height="43dp"
            android:layout_below="@+id/name_id"
            android:background="@drawable/customized_edtitext2"
            android:drawablePadding="15dp"
            android:paddingRight="10dp"
            android:paddingLeft="15dp"
            android:drawableRight="@mipmap/ic_unlocked"
            android:elevation="2dp"
            android:textStyle="bold"
            android:hint="@string/register_pass_commnet"
            android:fontFamily="@font/iransansmedium"
            android:textSize="12sp"
            android:paddingEnd="10dp" />

這是我的 php 頁面的 url在這里輸入圖片描述

還有更多信息:我安裝了 Genymotion,還有 bluestack 和 VirtaulBox。 他們在端口上有沖突嗎? 我的意思是這些軟件會導致 Apache xampp 及其端口發生沖突嗎?

我的 Xampp 正在使用端口: 80 ,如下圖所示。 所以在 IP 地址下面寫為http://10.0.2.2:80/對我http://10.0.2.2:80/

Xampp

如果您有 wifi 並且您的計算機和真實設備(手機)連接到同一個 wifi,您可以使用NGROK軟件工具在您的真實設備上運行和調試您的應用程序。

運行 ngrok.exe 並輸入ngrok http 80 ,它會給你一個如下圖所示的地址。

點擊

只需在您的應用程序中使用您使用 IP 地址的網址即可。 最好使用。

此答案假定瀏覽器屏幕截圖顯示頁面加載成功。 我相信,錯誤頁面有點灰色。


手頭可能存在防火牆問題。 由於您使用的是非標准端口,與 XAMPP 一起安裝的防火牆例外可能不會涵蓋這一點。

轉至 Windows 防火牆高級設置 -> 入站規則。 現在添加允許 TCP 端口 800 上的流量的規則。確保取消選中公共配置文件,以便如果您使用公共 wifi,則沒有人能夠連接到此應用程序。

現在使用智能手機的瀏覽器檢查您的 PHP 應用程序是否可訪問。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM