dream8877 2013-04-03 06:02
浏览 110

android + mysql +登录

i am creating a test for login proccess using php and mysql to connect to the database on android but i got in my log cat errors and on the emulator a force close can anyone help me with this??

AndroidLogin.java

package com.sencide;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AndroidLogin extends Activity implements OnClickListener {


     Button ok,back,exit;
     TextView result;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

     // Login button clicked
        ok = (Button)findViewById(R.id.btn_login);
        ok.setOnClickListener(this);

        result = (TextView)findViewById(R.id.lbl_result);



    }



    public void postLoginData() {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
         Log.e("Responce-->","after httpclient");
        /* login.php returns true if username and password is equal to saranga */
        HttpPost httppost = new HttpPost("http://192.168.1.64/login.php");
        Log.e("Responce-->","after httppost");
        try {
            // Add user name and password
         EditText uname = (EditText)findViewById(R.id.txt_username);
         String username = uname.getText().toString();

         EditText pword = (EditText)findViewById(R.id.txt_password);
         String password = pword.getText().toString();

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username", username));
            nameValuePairs.add(new BasicNameValuePair("password", password));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            Log.e("Responce-->","after using the list name pair");

            // Execute HTTP Post Request
            Log.w("SENCIDE", "Execute HTTP Post Request");
            HttpResponse response = httpclient.execute(httppost);
            Log.e("Responce-->","after execute the http response");
          //  String str = inputStreamToString(response.getEntity().getContent()).toString();
            String str = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
            //Log.w("SENCIDE", str);

            Log.e("Responce-->",""+str);

            if(str.toString().equalsIgnoreCase("true"))
            {
             Log.w("SENCIDE", "TRUE");
             result.setText("Login successful");  
            }else
            {
             Log.w("SENCIDE", "FALSE");
             result.setText(str);            
            }

        } catch (ClientProtocolException e) {
         e.printStackTrace();
        } catch (IOException e) {
         e.printStackTrace();
        }
    } 



    private StringBuilder inputStreamToString(InputStream is) {
        String line = "";
        StringBuilder total = new StringBuilder();
        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        // Read response until the end
        try {
         while ((line = rd.readLine()) != null) {
           total.append(line);
         }
        } catch (IOException e) {
         e.printStackTrace();
        }
        // Return full string
        return total;
       }

        /* login.php returns true if username and password is equal to saranga */


    @Override
    public void onClick(View view) {
        // TODO Auto-generated method stub
        if(view == ok){
             Thread t = new Thread(){
                    public void run(){
                        postLoginData();
                    }
                };
                t.start();

          }
    }





}

log_cat

    04-03 06:38:32.182: E/Responce-->(1222): after httpclient
04-03 06:38:32.472: E/Responce-->(1222): after httppost
04-03 06:38:32.522: E/Responce-->(1222): after using the list name pair
04-03 06:38:32.522: W/SENCIDE(1222): Execute HTTP Post Request
04-03 06:38:49.141: E/Responce-->(1222): after httpclient
04-03 06:38:49.151: E/Responce-->(1222): after httppost
04-03 06:38:49.163: E/Responce-->(1222): after using the list name pair
04-03 06:38:49.163: W/SENCIDE(1222): Execute HTTP Post Request
04-03 06:38:49.324: E/Responce-->(1222): after httpclient
04-03 06:38:49.324: E/Responce-->(1222): after httppost
04-03 06:38:49.331: E/Responce-->(1222): after using the list name pair
04-03 06:38:49.331: W/SENCIDE(1222): Execute HTTP Post Request
04-03 06:38:51.662: E/Responce-->(1222): after httpclient
04-03 06:38:51.662: E/Responce-->(1222): after httppost
04-03 06:38:51.662: E/Responce-->(1222): after using the list name pair
04-03 06:38:51.662: W/SENCIDE(1222): Execute HTTP Post Request
04-03 06:38:52.742: E/Responce-->(1222): after httpclient
04-03 06:38:52.742: E/Responce-->(1222): after httppost
04-03 06:38:52.762: E/Responce-->(1222): after using the list name pair
04-03 06:38:52.762: W/SENCIDE(1222): Execute HTTP Post Request

login.php

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="testlogin"; // Database name
$tbl_name="members"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form
$myusername=$_POST['username'];
$mypassword=$_POST['password'];

// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
echo "true";
}
else {
echo "Login Failed";
}
?>
  • 写回答

2条回答 默认 最新

  • douciping4283 2013-04-03 06:13
    关注

    your code is perfect just do:

    if you are using above 3.0 android OS then put this in onCreate

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
    

    or you can use AsyncTask check this link

    Insted of this

    String str = inputStreamToString(response.getEntity().getContent()).toString();
    

    use

    String str = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
    

    Print log first check what you get

    Log.e("Responce-->",""+str);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误