简体   繁体   English

Android与mysql数据库的连接

[英]Android connectivity with mysql database

Here is android code take permission in menifest i followed many videos but same result help me please 这是清单中的android代码获得许可,我关注了许多视频,但同样的结果请帮我

 Context ctx;
    String res;
    AlertDialog alertDialog;
    background(Context ctx)
    {
        this.ctx = ctx;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        alertDialog = new AlertDialog.Builder(ctx).create();
        alertDialog.setTitle("hellooooo");
    }

    @Override
    protected String doInBackground(String... params) {

        String url_response = "http://192.168.1.102/login.php";

        String id = params[0];

        try {
            URL url = new URL(url_response);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);

            OutputStream os = httpURLConnection.getOutputStream();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
            String data = URLEncoder.encode("id","UTF-8")+"="+URLEncoder.encode(id,"UTF-8");
            bw.write(data);
            bw.flush();
            os.close();

            InputStream is = httpURLConnection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is,"iso-8859-1"));
            String line ="";
             res = "";

            while((line = br.readLine())!=null)
            {
                res +=line;
            }
            return res;


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return "ffffff";
    }

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

php code for connectivity this is the php code for api which is 100 perecent correct and i checked it on localhost it worked when ever i try to connect to andriod whith mysql through this php code api then it does not work . 用于连接的php代码,这是api的php代码,它正确率为100%,我在localhost上对其进行了检查,当我尝试通过此php代码api连接到andriod whith mysql时,它就无法正常工作。 hava you people have any idea for thsi 哈瓦,你对这个人有任何想法

$mysql_qry = "select * from users where name like '$id';";
$result = mysqli_query($conn,$mysql_qry);
if(mysqli_num_rows($result)>0)
{
    echo "success";
}
else
{
    echo "not suces";
}
?>

output error enter image description here 输出错误在此处输入图像描述

Have you added the internet access permission to your manifest? 您是否已将互联网访问权限添加到清单中?

<uses-permission android:name="android.permission.INTERNET" />

Also, try sending the correct content type on the request: 另外,请尝试根据请求发送正确的内容类型:

httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

i suggest you to use volley library to do http transactions. 我建议您使用排球库进行http事务。 you can read everything you need about volley library in here http://code.tutsplus.com/tutorials/an-introduction-to-volley--cms-23800 您可以在http://code.tutsplus.com/tutorials/an-introduction-to-volley--cms-23800中阅读有关排球库的所有信息

you need to add volley library in your project. 您需要在项目中添加排球库。 in your app build.gradle , add this compile 'com.mcxiaoke.volley:library-aar:1.0.0' on dependecies. 在您的应用程序build.gradle中,根据需要添加此compile 'com.mcxiaoke.volley:library-aar:1.0.0'

create a method to use volley for http request and forget about asyntask because volley already handle it. 创建一种方法,以将volley用于http请求,并忽略asyntask,因为volley已经处理了它。

public void setRequestToServer (String mId){
    final String url_response = "http://192.168.1.102/login.php";

    final String id = mId;

    alertDialog = new AlertDialog.Builder(ctx).create();
    alertDialog.setTitle(response);

    StringRequest stringRequest = new StringRequest(Request.Method.POST, mUrl,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response){
                    if (!TextUtils.isEmpty(response)){
                        alertDialog.setMessage(response);
                    }else{
                        alertDialog.setMessage("Cannot received response from server");
                    }
                    alertDialog.show();

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("on Failure", error+"");
                    alertDialog.setMessage(error+"");
                    alertDialog.show();

                }
            }){
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();
            params.put("id",id));
            return params;
        }

    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

and on your php script , use $id to storage value from $_POST['id'] , like this : 然后在您的php脚本上,使用$ id从$_POST['id']存储值,如下所示:

$id = $_POST['id'];
$mysql_qry = "select * from users where name like '$id'";
$result = mysqli_query($conn,$mysql_qry);
if(mysqli_num_rows($result)>0)
{
    echo "success";
}
else
{
    echo "not suces";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM