duanseci1039 2013-05-28 02:47
浏览 47
已采纳

android设备xampp没有回报?

I have a very, very simple app. I've set it up using xampp. When I attempt to log a user into the localhost nothing happens, at all. No error, no logcat, nothing... very strange. I have tested XAMPP, it loads php pages into my browser, runs etc. XAMPP is definitely working as my localhost. I have tested the java class on an actual URL and it works. Here's the code:

connect.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);
} 
?>

test.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"; 
}
?>

login.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);
            }

I think it's my URL for the post. localhost/test.php. However I have tried the following

  • localhost/test.php
  • 10.0.0.2/test.php
  • 192.168.x.x/test.php
  • 127.0.0.1/test.php

None of them work. Probably a very simple solution and almost certainly a duplicate question but I cannot for the life of me figure this out.

EDIT

I changed the localhost to http://192.168.x.x/test.php and at first nothing... then finally I got a warning in logcat, like a caught exception. It says

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
  • 写回答

2条回答 默认 最新

  • doutuoji8418 2013-05-28 03:07
    关注

    Are you testing it with the emulator or your actual device?

    If Device: Pointing it to localhost is going to make the android device think it's sitting on the local server. So will 127.0.0.1. If the device is connected to to the same network as your XAMPP server, you have to point it to the server's internal IP. If not connected, you'll have to allow outside connections to get to that machine then point to the external IP.

    10.0.2.2 will only work if you are testing the app on an emulator. You can read more about it here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。