在Android中通过XAMPP连接到MYSQL

时间:2013-08-09 11:01:29

标签: php android mysql xampp wamp

我试图通过XAMP将我的Android应用程序与MYSQL连接起来。我一直在寻找其他类似的问题,但任何一个真的帮助了我。我试图在模拟器中运行代码,但它给我带来DB和其他东西的问题。 MYSQL中的表有2个字段,Sensors和Calibrated(两个字符串)。 cm.GetMobileUnits正常工作并返回两个字段的Pair。这是Android代码:

Thread thread = new Thread(new Runnable(){
        @Override
        public void run() {
            try {
                ArrayList<Pair<String, String>> stringList = cm.getMobileUnits("Mobile Device");
                //Log.d("MYSQL", "Tuplas adquiridas");
                String url="http://192.168.1.10/test.php";

                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);

                 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                 for (int i = 0; i < stringList.size(); i++) {
                     Pair<String, String> pair = stringList.get(i);
                     nameValuePairs.add(new BasicNameValuePair("Sensors", pair.first));
                     nameValuePairs.add(new BasicNameValuePair("Calibrated", pair.second));
                     httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  
                     HttpResponse httpResponse = httpClient.execute(httpPost);
                     httpResponse.getEntity().consumeContent();

                 }
                     } catch (UnsupportedEncodingException e) 
                     {
                 // TODO Auto-generated catch block
                 e.printStackTrace(); 
                    } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IllegalStateException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }     
        }
    });

我的PHP文件:

<?php
$con = mysql_connect("localhost","root","1234");
if (!$con)
 {
  die('Could not connect: ' . mysql_error());
 }

mysql_select_db("eHealthDroid", $con);

$sensors = $_POST['Sensors'];
$calibrated = $_POST['Calibrated'];

$sql = "INSERT INTO mobile_units(sensors,calibrated)values('$sensors', '$calibrated')";
      mysql_query($sql) or die(mysql_error());
mysql_close($con);

?>

现在我有另一个问题......为什么我会在手机上使用WIFI或3G时获得不同的输出?我应该使用哪一个?

使用wifi输出:

08-09 12:53:25.178: I/System.out(6926): [socket][0] connection /192.168.1.10:80;LocalPort=46210(0)
08-09 12:53:25.178: I/System.out(6926): [CDS]connect[/192.168.1.10:80] tm:90
08-09 12:53:25.179: D/Posix(6926): [Posix_connect Debug]Process com.pfc.ehealthdroid :80 
08-09 12:53:25.505: I/System.out(6926): [socket][/192.168.1.15:46210]
08-09 12:53:25.505: I/System.out(6926): rx timeout:0
08-09 12:53:25.594: I/System.out(6926): rx timeout:1
08-09 12:53:25.595: I/System.out(6926): [CDS]EAGAIN or EWOULDBLOCK in Recvfrom
08-09 12:53:25.595: I/System.out(6926): [CDS]read is 0
08-09 12:53:25.595: I/System.out(6926): rx timeout:0
08-09 12:53:25.595: I/System.out(6926): rx timeout:0
08-09 12:53:25.624: I/System.out(6926): rx timeout:1
08-09 12:53:25.625: I/System.out(6926): [CDS]EAGAIN or EWOULDBLOCK in Recvfrom
.....

3G输出:

08-09 12:56:42.024: I/System.out(7313): [socket][1:49104] exception
08-09 12:56:42.025: I/System.out(7313): [CDS]close[49104]
08-09 12:56:42.025: I/System.out(7313): close [socket][/0.0.0.0:49104]
08-09 12:56:42.025: W/System.err(7313): org.apache.http.conn.ConnectTimeoutException: Connect to /192.168.1.10:80 timed out
08-09 12:56:42.026: W/System.err(7313):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
08-09 12:56:42.026: W/System.err(7313):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
08-09 12:56:42.026: W/System.err(7313):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-09 12:56:42.026: W/System.err(7313):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-09 12:56:42.026: W/System.err(7313):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:363)
08-09 12:56:42.026: W/System.err(7313):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-09 12:56:42.026: W/System.err(7313):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-09 12:56:42.027: W/System.err(7313):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-09 12:56:42.027: W/System.err(7313):     at com.pfc.ehealthdroid.MainActivity$1.run(MainActivity.java:362)
08-09 12:56:42.027: W/System.err(7313):     at java.lang.Thread.run(Thread.java:856)
08-09 12:56:42.027: D/dalvikvm(7313): threadid=11: exiting

任何帮助都会很棒!感谢

2 个答案:

答案 0 :(得分:0)

我是否理解您192.168.1.15是您的IDE的PC-Localhost并且您想通过在模拟器中运行的Androidapp连接?如果是,则必须使用“10.0.2.2”作为IP地址。在模拟器中,使用IDE将地址重定向到您的电脑

未连接到IP,您还必须允许您的应用使用互联网连接:

答案 1 :(得分:0)

好的,这是令人尴尬的,但至少我找到了解决方案。

我将php文件放在php / www目录中,说实话我不知道为什么我认为这是正确的目录。我检查了默认目录,我发现是httpdocs和...瞧!