android中的asyncTask用于从MySQL数据库中检索数据

时间:2014-05-21 10:30:25

标签: php android html mysql android-asynctask

请设置我的wampserver并通过wifi配置我的Android设备以正确访问数据库和服务器,它运行良好。我想从wampserver中的数据库中选择数据,并在TextView中显示数据,我使用asyncTask来执行此操作,这似乎正在起作用。现在问题是结果。我已经尝试了几个选项来操纵这个但我仍然得到"
" TextView或Toast消息中的换行符。我可以弄清楚为什么我仍然得到这个回报。任何帮助将非常感谢。谢谢你

     class SingmeIn extends AsyncTask<String, String, String>{

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
         try{
                String username = u;
                String password = p;
                String link = "http://192.168.30.1/androidphp/loginget.php?username=" +username+"&password="+password;
                URL url = new URL(link);
                HttpClient client = new DefaultHttpClient();
                HttpGet request = new HttpGet();
                request.setURI(new URI(link));
                HttpResponse response = client.execute(request);

                BufferedReader in = new BufferedReader
               (new InputStreamReader(response.getEntity().getContent()));

               StringBuffer sb = new StringBuffer("");
               String line="";
               String[] data=null;         
               while((line=in.readLine())!=null){
                   data=line;
                  break;
                }
                in.close();
                return data[0];   
    }
    catch(Exception e)
    {
    return new String("Exception: " + e.getMessage());
    }

     }
    @Override
       protected void onPostExecute(String result){
          //statusField.setText("Login Successful");
         role.setText(result);
         //System.out.println(result);
         Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
       }
}

这是我的PHP代码:

       <?php
    $con=mysqli_connect("192.168.30.1","root"," ","searchmedb");
   if (mysqli_connect_errno($con))
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
        $username = $_POST['username'];
        $password = $_POST['password'];
        $result = mysqli_query($con,"SELECT UserType FROM accounts where    UserName='$username'                  and UserPassword='$password'");
    $row = mysqli_fetch_array($result);
    $data = $row[0];
    if($data){
    echo $data;
    }
    mysqli_close($con);
    ?>

最初我在我的while循环中使用它,这也不起作用:

    while ((line = in.readLine()) != null) {
                  sb.append(line +"\n");
                  break;
                }
                in.close();
                return sb.toString();   

1 个答案:

答案 0 :(得分:0)

你的PHP代码返回一些额外的空间。检查你的php文件行的末尾。