Android与mysql数据库的连接

时间:2016-04-10 07:27:25

标签: php android mysql

这是Android代码获取权限的清单我跟随很多视频,但同样的结果帮助我

<ol>
  <li>hi</li>
  <ol type="I">
    <li>Hi</li>
    <ol type="a">
      <li>hi</li>
    </ol>
    <li>Hi</li>
    <li>Ect.</li>
  </ol>
</ol>

连接的PHP代码 这是api的PHP代码,这是100 perecent正确,我检查它localhost它工作,当我尝试连接到andriod whith mysql通过这个PHP代码api然后它不起作用。哈瓦,你们对这个人有任何想法     

 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();
}

输出错误 enter image description here

2 个答案:

答案 0 :(得分:0)

您是否已为展示添加了互联网访问权限?

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

另外,请尝试在请求中发送正确的内容类型:

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

答案 1 :(得分:0)

我建议你使用volley库来做http事务。 你可以在http://code.tutsplus.com/tutorials/an-introduction-to-volley--cms-23800

阅读有关排球库的所有信息

您需要在项目中添加排球库。在你的app build.gradle中,在dependecies上添加compile 'com.mcxiaoke.volley:library-aar:1.0.0'

创建一个方法来使用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);
}

在你的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";
}