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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度