douzhanglu4591 2015-01-21 11:34
浏览 53
已采纳

将Android连接到MySQL

So I'm really new to this. I was following a tutorial on web that showed how to connect these two. But everything I do it just jumps to catch part and it says "No connection". What am I missing? I also connect to a http server to use a PHP script which queries username and password from the database.

try
    {
        nameValuePairs = new ArrayList<NameValuePair>();

        nameValuePairs.add(new BasicNameValuePair("username", username));
        nameValuePairs.add(new BasicNameValuePair("password", password));

        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        response = httpClient.execute(httpPost);

        if(response.getStatusLine().getStatusCode()==200)
        {
            entity = response.getEntity();

            if(entity != null)
            {
                InputStream instream = entity.getContent();

                JSONObject jsonResponse = new JSONObject(instream.toString());

                String retUser = jsonResponse.getString("upime");
                String retPass = jsonResponse.getString("geslo");

                if(username.equals(retUser) && password.equals(retPass))
                {
                    SharedPreferences sp = getSharedPreferences("logindetails", 0);

                    SharedPreferences.Editor spedit = sp.edit();

                    spedit.putString("upime", username);
                    spedit.putString("geslo", password);

                    spedit.commit();

                    Toast.makeText(getBaseContext(), "Login successful", Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(getBaseContext(), "Failed to login", Toast.LENGTH_SHORT).show();
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(getBaseContext(), "No connection", Toast.LENGTH_LONG).show();
    }
  • 写回答

3条回答 默认 最新

  • doumeinuoye81969 2015-01-21 13:52
    关注
     <?php 
     header('Content-Type: application/json'); 
     $dbhost ="localhost"; 
     $dbuser = "username"; 
     $dbpass = "password"; 
     $dbdb = "db"; 
     $connect = mysql_connect($db_host, $dbuser, $dbpass) or die("connection error"); 
    
     mysql_select_db($dbdb);
     $username =$_POST['username'];
     $password=$_POST['password'];
     $query = mysql_query("SELECT * FROM users WHERE user='$username' AND pass='$password'");
    
      $num = mysql_num_rows($query);
     if($num == 1) 
    { 
     while($list=mysql_fetch_assoc($query))
     { $output = $list; 
     }
      mysql_close(); 
    
    
     echo json_encode($output);
    
    
    ?>
    

    edit the final "echou" to echo and add

        header('Content-Type: application/json'); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗