-2

嗨,我无法让这个应用程序登录到我的网站。这是php代码:

<?php
            session_start();

            include 'api/connect.php';

            $username = mysql_real_escape_string(htmlentities($_POST['username']));
            $password = mysql_real_escape_string(htmlentities(sha1($_POST['password'])));

            $query = mysql_query("SELECT * FROM `beta` WHERE `username` = '" . $username . "' AND `password` = '" . $password . "'") or die(mysql_query());

            // Start the long process
            $rows = mysql_num_rows($query);

            if($rows == 1){     
              while($list = mysql_fetch_assoc($query)) {
                 $output = $list;
                 echo json_encode($output);
              }
              mysql_close();
            }


?>

这是安卓代码:

package com.awsomechat;

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

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.json.JSONObject;


import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
 import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

EditText etUser, etPass;
Button blogin;

// Username, Password
String username, password;

// Make an HTTP Client
HttpClient httpclient;
HttpPost httppost;

// Store the username and password in an array
ArrayList<NameValuePair> nameValuePairs;

// HTTP Response & Entity
HttpResponse response;
HttpEntity entity;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initialise();
}

private void initialise() {
    // TODO Auto-generated method stub
    etUser = (EditText) findViewById(R.id.etUser);
    etPass = (EditText) findViewById(R.id.etPass);
    blogin = (Button) findViewById(R.id.etSubmit);
    blogin.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // The user tapped the Login button, start logging in

    // Default HTTPClient
    httpclient = new DefaultHttpClient();

    // Post the values to the AwsomeChat Login script
    httppost = new HttpPost("http://beta-awsomechat.tk/login.php");

    // THe values we are working with
    username = etUser.getText().toString();
    password = etPass.getText().toString();

    // Try to login. Start the login validation/process
    try {
        nameValuePairs = new ArrayList<NameValuePair>();

        // Store the username and password in an array
        nameValuePairs.add(new BasicNameValuePair("username", username));
        nameValuePairs.add(new BasicNameValuePair("password", password));

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        response = httpclient.execute(httppost);

        if(response.getStatusLine().getStatusCode() == 200){

            // Get the info given to us.
            entity = response.getEntity();

            if(entity != null){
                InputStream instream = entity.getContent();

                JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));

                String retUser = jsonResponse.getString("username");
                String retPass = jsonResponse.getString("password");

                // Start the validation process
                if(username.equals(retUser) && password.equals(retPass)){
                    //
                    //
                    SharedPreferences sp = getSharedPreferences("logindetails", 0);

                    SharedPreferences.Editor spedit = sp.edit();

                    spedit.putString("user", username);
                    spedit.putString("pass", password);

                    //
                    spedit.commit();

                    Toast.makeText(getBaseContext(), "Login Success", Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(getBaseContext(), "Login Fialed.", Toast.LENGTH_SHORT).show();
                }
            }
        }
    } catch(Exception e) {
        e.printStackTrace();
        Toast.makeText(getBaseContext(), "Login Failed.", Toast.LENGTH_SHORT).show();
    }
}

private static String convertStreamToString(InputStream is) {
    /*
     * To convert the InputStream to String we use the BufferedReader.readLine()
     * method. We iterate until the BufferedReader return null which means
     * there's no more data to read. Each line will appended to a StringBuilder
     * and returned as String.
     */
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}


}

由于某种原因,它每次都说“登录失败”。我修复了php代码中的所有错误,它必须是android代码,因为php代码中没有错误。LogCat 什么也没说,Console 也没有,这使得发现问题变得更加困难。

**编辑:我在 LogCat 中遇到错误:

06-15 16:55:16.400: E/ActivityThread(870): Service com.android.exchange.ExchangeService 已泄露 ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cfaac0 最初绑定在这里 06-15 16: 55:16.400: E/ActivityThread(870): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService 已泄露 ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cfaac0 最初绑定在这里 06-15 16 :55:16.400: E/ActivityThread(870): 在 android.app.LoadedApk$ServiceDispatcher.(LoadedApk.java:969) 06-15 16:55:16.400: E/ActivityThread(870): 在 android.app.LoadedApk .getServiceDispatcher(LoadedApk.java:863) 06-15 16:55:16.400: E/ActivityThread(870): 在 android.app.ContextImpl.bindService(ContextImpl.java:1418) 06-15 16:55:16.400: E /ActivityThread(870):在 android.app。ContextImpl.bindService(ContextImpl.java:1407) 06-15 16:55:16.400: E/ActivityThread(870): 在 android.content.ContextWrapper.bindService(ContextWrapper.java:473) 06-15 16:55:16.400: E/ActivityThread(870): 在 com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157) 06-15 16:55:16.400: E/ActivityThread(870): 在 com.android.emailcommon.service .ServiceProxy.setTask(ServiceProxy.java:145) 06-15 16:55:16.400: E/ActivityThread(870): at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191) 06-15 16 :55:16.400: E/ActivityThread(870): 在 com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850) 06-15 16:55:16.400: E/ActivityThread(870): 在 com.android .emailcommon.utility.Utility$2.doInBackground(Utility.java:551) 06-15 16:55:16.400: E/ActivityThread(870): at com.android.emailcommon.utility.Utility$2。doInBackground(Utility.java:549) 06-15 16:55:16.400: E/ActivityThread(870): at android.os.AsyncTask$2.call(AsyncTask.java:287) 06-15 16:55:16.400: E /ActivityThread(870): at java.util.concurrent.FutureTask.run(FutureTask.java:234) 06-15 16:55:16.400: E/ActivityThread(870): at java.util.concurrent.ThreadPoolExecutor.runWorker( ThreadPoolExecutor.java:1080) 06-15 16:55:16.400: E/ActivityThread(870): 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 06-15 16:55:16.400: E/ActivityThread(870): 在 java.lang.Thread.run(Thread.java:856) 06-15 16:55:16.514: E/StrictMode(870): null 06-15 16:55:16.514: E/ StrictMode(870): android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService 已泄露 ServiceConnection com.android.emailcommon.service。最初绑定在这里的 ServiceProxy$ProxyConnection@40cfaac0 06-15 16:55:16.514: E/StrictMode(870): at android.app.LoadedApk$ServiceDispatcher.(LoadedApk.java:969) 06-15 16:55:16.514 : E/StrictMode(870): 在 android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863) 06-15 16:55:16.514: E/StrictMode(870): 在 android.app.ContextImpl.bindService(ContextImpl. java:1418) 06-15 16:55:16.514: E/StrictMode(870): 在 android.app.ContextImpl.bindService(ContextImpl.java:1407) 06-15 16:55:16.514: E/StrictMode(870) : 在 android.content.ContextWrapper.bindService(ContextWrapper.java:473) 06-15 16:55:16.514: E/StrictMode(870): 在 com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157 ) 06-15 16:55:16.514: E/StrictMode(870): 在 com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145) 06-15 16:55:16.514:E/StrictMode(870): 在 com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191) 06-15 16:55:16.514: E/StrictMode(870): 在 com.android.exchange.ExchangeService $7.run(ExchangeService.java:1850) 06-15 16:55:16.514: E/StrictMode(870): at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551) 06-15 16 :55:16.514: E/StrictMode(870): 在 com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549) 06-15 16:55:16.514: E/StrictMode(870): 在 android .os.AsyncTask$2.call(AsyncTask.java:287) 06-15 16:55:16.514: E/StrictMode(870): 在 java.util.concurrent.FutureTask.run(FutureTask.java:234) 06-15 16:55:16.514: E/StrictMode(870): 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 06-15 16:55:16.514: E/StrictMode(870): 在 java.util .concurrent.ThreadPoolExecutor$Worker。运行(ThreadPoolExecutor.java:573)06-15 16:55:16.514:E/StrictMode(870):在 java.lang.Thread.run(Thread.java:856)06-15 16:55:28.370:E/ SurfaceFlinger(37): ro.sf.lcd_density 必须定义为构建属性

按下按钮发送登录详细信息时,Log Cat 中总是会弹出此错误:

06-15 16:59:44.379: E/SurfaceFlinger(37): ro.sf.lcd_density 必须定义为构建属性**

4

1 回答 1

0

我认为问题出在您的 retUser 和 retPassword 上。您是否尝试过调试以查看它们的价值?我怀疑它们为空,因此在将它们与用户名和密码进行比较时返回 false。当参数为空时,字符串的 equals 方法返回 false。这使检查失败并进入 else 块。在这里,您显示吐司“登录失败”。无需将堆栈跟踪打印到 LogCat 或控制台。

我首先会调试 PHP 代码,看看它是否真的返回 JSON 编码的消息。如果是这样,请确保将字节流正确转换为 JSONObject。

编辑

正如我在您的一条评论中所读到的,您在将密码存储到数据库之前已经使用 PHP 对密码进行了哈希处理。因此,您需要在 java 中使用相同的散列算法对输入的密码进行散列,然后比较散列。如果它们相等,则提供的密码与从数据库中检索到的密码相同。

于 2013-06-15T16:04:30.893 回答