-2

我目前正在使用 Android SDK 和 PHP 服务器端脚本为我的网站开发应用程序。

我正在尝试通过 a和请求发送 POST 数据username并获取结果()。不幸的是,它无法正常工作。请求已发送,结果由我的 PHP 脚本发回,但尚未工作。passwordHttpClientHttpPostecho $result;HttpResponseIntent

这是我的 PHP 代码(我隐藏了我的数据库信息,你会明白为什么!):

<?php
    $con = mysql_connect('localhost','','');
    mysql_select_db('', $con);

    $username = $_POST['username'];
    $password = $_POST['password'];

    $users = mysql_query("SELECT * FROM `` WHERE `username`='".$username."' AND `password`='".$password."'");
    if (mysql_num_rows($users) == 1)
    {
        $usr = mysql_fetch_array($users);
        $usr['username'];
    }
    else
    {
        echo "error";
    }
?>

还有我的 Javalogin()方法:

public void login()
    {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.creationsmicroweb.com/app/login.php");

        String username = (String) findViewById(R.id.usr1).toString();
        String password = (String) findViewById(R.id.pwd1).toString();
        Button login = (Button) findViewById(R.id.button1);

        try
        {
            // Disabling other request
            login.setEnabled(false);
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username", username));
            nameValuePairs.add(new BasicNameValuePair("password", password));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            Log.w("http", "Executing HTTP Post Request");
            HttpResponse response = httpclient.execute(httppost);

            String status = inputStreamToString(response.getEntity().getContent()).toString();
            Integer code = response.getStatusLine().getStatusCode();

            if (!status.toString().equalsIgnoreCase("error"))
            {
                Intent myIntent = new Intent(this.getBaseContext(), HomeActivity.class);
                myIntent.putExtra("username", status);
                startActivityForResult(myIntent, 0);
                Toast.makeText(this, "You've been connected!", Toast.LENGTH_LONG).show();
                Log.i("login", "Logged in // " + response);
            }
            else
            {
                Toast.makeText(this, "Bad username/password", Toast.LENGTH_LONG);
                Log.i("login", "Bad username/password // " + response);
                login.setEnabled(true);
            }

        }
        catch (ClientProtocolException e)
        {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
            Log.e("http", e.getMessage());
            login.setEnabled(true);
        }
        catch (IOException e)
        {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
            Log.e("http", e.getMessage());
            login.setEnabled(true);
        }
    }

    private Object inputStreamToString(InputStream is)
    {
        // TODO Auto-generated method stub
        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;
    }

PHP 返回的响应无法与我的 Java 代码一起正常工作。很明显,当用户按下“登录”时,凭据被发送到 PHP,然后 PHP 返回一个答案(用户名或错误)。之后,Java 代码检查答案是否等于“错误”,但这不起作用。登录成功(我记录了所有内容),但 Intent 方法没有执行,并且没有显示 Toast 消息。

很明显,我想知道为什么请求不能正常工作。我从 Java 开始,所以我不是最好的!

4

1 回答 1

0

你有看到Toasts吗?如果不是,那么我认为您可能context对他们也有错误的这一行:

Toast.makeText(this, "Bad username/password", Toast.LENGTH_LONG);

你错过了.show()

如果这是在视图中,则使用getContext()orgetBaseContext()代替this.

我建议您使用Log.d()记录响应并检查它是否符合您的期望。

于 2012-07-16T12:27:04.853 回答