简体   繁体   中英

asyncTask in android for retrieving data from MySQL database

Please I have set up my wampserver and configure my android device over wifi to access the database and server correctly and it is working well. I want to select data from the database in the wampserver and display the data in a TextView and I am using an asyncTask to perform this operation which seems to be working.Now the problem is the results. I have tried several options to manipulate this yet I am still getting "
" a line break in the TextView or Toast msg. I can figure out why I am still getting this a return. Any assistance will be very appreciated.Thank you

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

This is my PHP code :

       <?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);
    ?>

Initially I was using this in my while loop, which did not also work:

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

You PHP code returning some additional space. Check at the end of the line of your php file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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