繁体   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