登录网站无法正常工作

时间:2013-06-15 15:46:06

标签: java android

您好,我无法让此应用登录我的网站。这是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();
            }


?>

这是android代码:

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


}

出于某种原因,它一直说&#34;登录失败&#34;每次。我修复了PHP代码中的所有错误,它必须是android代码,因为php代码中没有错误。 LogCat什么也没说,控制台也没有找到问题。

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

06-15 16:55:16.400:E / ActivityThread(870):服务com.android.exchange.ExchangeService泄露了最初绑定的ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cfaac0 06-15 16:55:16.400:E / ActivityThread(870):android.app.ServiceConnectionLeaked:服务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):at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157) 06-15 16:55:16.400:E / ActivityThread(870):at 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):at com.android.exchange.ExchangeService $ 7.run(ExchangeService.java:1850) 06-15 16:55:16.400:E / ActivityThread(870):at 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):在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):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:573) 06-15 16:55:16.400:E / ActivityThread(870):at 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:服务com.android.exchange.ExchangeService泄漏了最初绑定的ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cfaac0这里 06-15 16:55:16.514:E / StrictMode(870):在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):at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157) 06-15 16:55:16.514:E / StrictMode(870):at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145) 06-15 16:55:16.514:E / StrictMode(870):at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191) 06-15 16:55:16.514:E / StrictMode(870):at 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):at 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):at java.util.concurrent.FutureTask.run(FutureTask.java:234) 06-15 16:55:16.514:E / StrictMode(870):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 06-15 16:55:16.514:E / StrictMode(870):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:573) 06-15 16:55:16.514:E / StrictMode(870):at 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必须定义为构建属性**

1 个答案:

答案 0 :(得分:0)

我认为问题是你的retUser和retPassword。您是否尝试过调试才能看到它们的价值?我怀疑它们是null,因此在将它们与用户名和密码进行比较时返回false。当参数为null时,String的等于methode返回false。这使得检查失败并转到else-block。在这里你显示吐司“登录失败”。无需将堆栈跟踪打印到LogCat或控制台。

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

修改

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