2012-05-27 04:03
浏览 49


I have a problem connecting database with Android app. I am trying to implement this tutorial. Everything seems to be fine but I neither get any success not an error.

There is a button listener which on clicking does a post to a PHP file and gets the result. Here is the code for it:-

    ok.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
            postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));
            //String valid = "1";
            String response = null;
            try {
                response = CustomHttpClient.executeHttpPost("", postParameters);
                String res=response.toString();
                Log.d("res:", res);

               // res = res.trim();
                res= res.replaceAll("\\s+","");                              

                    error.setText("Correct Username or Password");
                    error.setText("Sorry!! Incorrect Username or Password"); 
            } catch (Exception e) {


Here is the http post method:-

public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
    BufferedReader in = null;
    try {
        HttpClient client = getHttpClient();
        HttpPost request = new HttpPost(url);
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);

        String result = sb.toString();
        Log.d("postMethodReturn", result);
        return result;
    } finally {
        if (in != null) {
            try {
            } catch (IOException e) {

The PHP code is as below:-

//connect to the db
$user = "xyz";
$pswd = "xyz";
$db = "mydb";
$host = "localhost";
$conn = mysql_connect($host, $user, $pswd);
//run the query to search for the username and password the match  
$query = "SELECT * FROM mytable WHERE user = '$un' AND pass = '$pw'";
$result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());
//this is where the actual verification happens  
if(mysql_num_rows($result) --> 0)  
echo 1;  // for correct login response  
echo 0; // for incorrect login response  

Is there any bug in the program? I tried logging the intermediate values of res (http response) in activity code and result in the execute post method, but nothing is being logged. Tried changing "localhost" to "" and also into a publicly available webhost, with all the database environment, but no success. All these on emulator and with public host, tried with real device too. Server seems to be running when checked from browser. Database exists with the values. All services running (apache, mysql).

The main problem is that there is no error! Any suggestions what is going wrong? Couldn't find anyone with the same problem.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongsi8812
    dongsi8812 2012-05-27 08:41

    the problem was --> in the PHP code. changed it to == or > and everything works fine!

    点赞 评论