0

我有一个非常非常简单的应用程序。我已经使用 xampp 进行了设置。当我尝试将用户登录到本地主机时,根本没有任何反应。没有错误,没有 logcat,什么都没有……很奇怪。我已经测试了 XAMPP,它将 php 页面加载到我的浏览器中,运行等。XAMPP 绝对是我的本地主机。我已经java在一个实际的 URL 上测试了这个类,它可以工作。这是代码:

连接.php

<?php
$host = 'localhost';
$user = 'root';
$pass = '';
$db = 'test';
$conn_error = 'Not Connected!';

if (!mysql_connect($host, $user, $pass) || !mysql_select_db($db)) {
    die ($conn_error);
} 
?>

测试.php

<?php
require_once 'connect.php';

$username = $_POST['username'];
$password = $_POST['password']; 
$query_search = "SELECT * FROM members WHERE username = '".$username."' AND password = '".$password. "'"; 
$query_exec = mysql_query($query_search) or die(mysql_error());
$rows = mysql_num_rows($query_exec);

if($rows == 1) {
     echo "1"; 
} else {
    echo "Username/Password is invalid"; 
}
?>

登录.java

class LogMeIn extends AsyncTask<String, Void, String> {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://localhost/test.php");

    protected String doInBackground(String... urls) {

        try {
            username = un.getText().toString();
            password = pw.getText().toString();
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                    2);
            nameValuePairs
                    .add(new BasicNameValuePair("username", username));
            nameValuePairs
                    .add(new BasicNameValuePair("password", password));
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = client.execute(post);
            String res = inputStream(response.getEntity().getContent())
                    .toString();
            Log.v("RESPONSE", res);

            // if username and password are valid, launch main activity
            if (res.toString() == "1") {
                Intent logIn = new Intent(getApplicationContext(),
                        Main.class);
                startActivity(logIn);
            }

我认为这是我的帖子网址。localhost/test.php. 但是我尝试了以下

  • 本地主机/test.php
  • 10.0.0.2/test.php
  • 192.168.xx/test.php
  • 127.0.0.1/test.php

它们都不起作用。可能是一个非常简单的解决方案,几乎可以肯定是一个重复的问题,但我一生都无法弄清楚这一点。

编辑

我将 localhost 更改为http://192.168.xx/test.php,起初什么也没有……最后我在 logcat 中收到了警告,就像捕获的异常一样。它说

05-27 23:38:42.510: W/System.err(13310): org.apache.http.conn.HttpHostConnectException: Connection to http://192.68.x.x refused
05-27 23:38:42.510: W/System.err(13310):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
05-27 23:38:42.510: W/System.err(13310):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-27 23:38:42.510: W/System.err(13310):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-27 23:38:42.510: W/System.err(13310):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-27 23:38:42.520: W/System.err(13310):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-27 23:38:42.520: W/System.err(13310):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-27 23:38:42.520: W/System.err(13310):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-27 23:38:42.520: W/System.err(13310):    at com.facilitysolutionsinc.trackflex.Log_In$LogMeIn.doInBackground(Log_In.java:83)
05-27 23:38:42.520: W/System.err(13310):    at com.facilitysolutionsinc.trackflex.Log_In$LogMeIn.doInBackground(Log_In.java:1)
05-27 23:38:42.520: W/System.err(13310):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-27 23:38:42.520: W/System.err(13310):    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-27 23:38:42.520: W/System.err(13310):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-27 23:38:42.520: W/System.err(13310):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-27 23:38:42.520: W/System.err(13310):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-27 23:38:42.520: W/System.err(13310):    at java.lang.Thread.run(Thread.java:856)
05-27 23:38:42.520: W/System.err(13310): Caused by: java.net.ConnectException: failed to connect to /192.68.x.x (port 80): connect failed: ETIMEDOUT (Connection timed out)
05-27 23:38:42.530: W/System.err(13310):    at libcore.io.IoBridge.connect(IoBridge.java:114)
05-27 23:38:42.530: W/System.err(13310):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
05-27 23:38:42.530: W/System.err(13310):    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
05-27 23:38:42.530: W/System.err(13310):    at java.net.Socket.connect(Socket.java:842)
05-27 23:38:42.530: W/System.err(13310):    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
05-27 23:38:42.530: W/System.err(13310):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
05-27 23:38:42.530: W/System.err(13310):    ... 14 more
05-27 23:38:42.530: W/System.err(13310): Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
05-27 23:38:42.540: W/System.err(13310):    at libcore.io.Posix.connect(Native Method)
05-27 23:38:42.540: W/System.err(13310):    at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
05-27 23:38:42.540: W/System.err(13310):    at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
05-27 23:38:42.540: W/System.err(13310):    at libcore.io.IoBridge.connect(IoBridge.java:112)
05-27 23:38:42.540: W/System.err(13310):    ... 19 more
4

2 回答 2

2

你是用模拟器还是你的实际设备测试它?

If Device: Pointing it to localhost 会使 android 设备认为它位于本地服务器上。127.0.0.1 也是如此。如果设备与 XAMPP 服务器连接到同一网络,则必须将其指向服务器的内部 IP。如果未连接,则必须允许外部连接到达该计算机,然后指向外部 IP。

10.0.2.2 仅在您在模拟器上测试应用程序时才有效。你可以在这里阅读更多关于它的信息。

于 2013-05-28T03:07:03.047 回答
0

我想告诉我的分析:D

我认为你的问题在于这段代码:

String res = inputStream(response.getEntity().getContent()).toString();

因为您不能通过使用.toString()方法直接将输入流转换为字符串。

要将 inputStream 转换为字符串,这里有一个小片段:

public static String responseToString(HttpResponse response){
            String result = "";
            try{
                InputStream in = response.getEntity().getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                StringBuilder str = new StringBuilder();
                String line = null;
                while((line = reader.readLine()) != null){
                    str.append(line + "\n");
                }
                in.close();
                result = str.toString();
            }catch(Exception ex){
                result = "Error";
            }
            return result;
        }

然后将下面的代码修改HttpResponse response = client.execute(post);为:

 HttpResponse response = client.execute(post);
 String res = responseToString(response); // add .trim().replace("\n", "") if you have newlines

我希望这是有帮助的,祝你好运!!^^

于 2013-05-28T03:15:36.973 回答