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";
}
?>
dounuo9921
dounuo9921 从DDMS->文件资源管理器导出此文件。/data/anr/traces.txt
7 年多之前 回复

2个回答



你的代码是完美的只做:</ p>

如果你使用上面的 3.0 android OS </ code>然后把它放在 onCreate </ code> </ p>

  StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()。permitAll()。build  (); 
StrictMode.setThreadPolicy(policy);
</ code> </ pre>

或者你可以使用 AsyncTask </ code>检查这个链接 </ p>

Insted of this </ p>
\ n

  String str = inputStreamToString(response.getEntity()。getContent())。toString(); 
</ code> </ pre>

使用</ p> \ n

  String str = EntityUtils.toString(response.getEntity(),HTTP.UTF_8); 
</ code> </ pre>

打印日志首先检查你的内容 得到</ p>

  Log.e(“Responce  - &gt;”,“”+ str); 
</ code> </ pre>
</ div>

展开原文

原文

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

dongtou8736
dongtou8736 Dhaval Sodha Parmaon浏览器我无法连接和模拟器浏览器我不上网我无法让互联网在模拟器上工作这是另一个问题抱歉你的时间你帮我很多但现在我不会打扰 你有这个问题抱歉
7 年多之前 回复
duanju9104
duanju9104 在浏览器中打开它:192.168.1.64/login.php检查你得到了什么?
7 年多之前 回复
doutangu4671
doutangu4671 好的先生我使用后台线程,但仍然无法得到正确的响应我认为错误是在执行hhtpresponse(编辑上面的log_cat和android login.java)
7 年多之前 回复
dpc57092
dpc57092 你得到keyDispatchingTimedOut所以把你的方法放在异步任务检查此链接:stackoverflow.com/questions/3467205 / ...和stackoverflow.com/questions/3467205 / ...
7 年多之前 回复
dqf42223
dqf42223 好吧,我使用严格模式,我在log_cat和模拟器上得到了一个力量关闭(见上面的日志猫)
7 年多之前 回复
douse8732
douse8732 log cat没有进入Log.e(“Responce - >”,“”+ str); 所以我认为错误就在此之前
7 年多之前 回复
dounei9043
dounei9043 我仍然在日志猫得到错误看到上面的日志猫(我是新来的android所以我需要帮助)
7 年多之前 回复



查看log_cat中的时间(甚至是编辑前的旧时间),似乎 httpclient.execute </ 代码>调用永远不会回来/超时。</ p>

模拟器是否可以访问login.php,login.php是否正常工作?

使用模拟器上的浏览器进行检查</ p>

除此之外,您不应该在onClick处理程序中执行任何昂贵的工作。</ p>
</ div>

展开原文

原文

Looking at the times in the log_cat (even the old one before you edited) it seems that the httpclient.execute call never comes back/times out.

Do the emulator have access to the login.php, and login.php is working correctly? Check with the Browser on the emulator

Beside that you should NOT do any time expensive stuff in the onClick handler.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐