从Android发布数据(模拟器) - 实施问题

时间:2014-05-24 09:45:09

标签: java android client-server httpclient

我的应用程序生成一个UDID并将其发送到服务器端。但是,在服务器端,$ _POST变量为空。我的PHP代码正在运行,因为我已经尝试了测试数据。 Android代码有问题,但我无法弄明白。我在模拟器上运行应用程序。

SecondAct.java -

public class SecondAct extends Activity {
private String android_id;

public String getUDID(){
    android_id = Secure.getString(getBaseContext().getContentResolver(), Secure.ANDROID_ID);
    return android_id;
}

public void PostServer(View view){

    ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if(networkInfo != null && networkInfo.isConnected()){
        //send data
        new PostHttp(getUDID()).execute();
    }
    else{
        //display error
    }
}

PostHttp.java -

public class PostHttp extends AsyncTask<Void,Integer,Void>  {
private String id = null;

PostHttp(String s){
    id = s;
}
protected Void doInBackground(Void... params) {
    // TODO Auto-generated method stub

    //Create the HTTP Post
    HttpParams httpParameters = new BasicHttpParams(); 

    //Setup timeouts -- for later

    HttpClient httpclient = new DefaultHttpClient(httpParameters);
    HttpPost httppost = new HttpPost(webphp);

    try{
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("id",id));
    nameValuePairs.add(new BasicNameValuePair("time",time));    
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity resEntity = response.getEntity();
    String result = EntityUtils.toString(resEntity);

    System.out.println(result);

 } catch (Exception e){
           e.printStackTrace();
    }

    return null;    
}

Geo.php -

<?
mysql_connect($host, $username, $password);
mysql_select_db($database) or die("Unable to connect to database");
$query = "INSERT INTO UDID (Time, UDID) VALUES (".$_POST['time'].", ".$_POST['id'].");";
$result = mysql_query($query) or die("Unable to execute query");

echo $_POST;
echo $_POST["id"];
?>

LogCat -

05-25 03:48:07.176: I/System.out(930): string(19) "2014-05-25 03:48:06"
05-25 03:48:07.176: I/System.out(930): string(16) "2bd5d455facd7fa2"
05-25 03:48:07.186: I/System.out(930): array(3) {
05-25 03:48:07.186: I/System.out(930):   ["id"]=>
05-25 03:48:07.186: I/System.out(930):   string(16) "2bd5d455facd7fa2"
05-25 03:48:07.186: I/System.out(930):   ["time"]=>
05-25 03:48:07.186: I/System.out(930):   string(19) "2014-05-25 03:48:06"
05-25 03:48:07.186: I/System.out(930):   ["extra"]=>
05-25 03:48:07.186: I/System.out(930):   string(4) "xxxx"
05-25 03:48:07.196: I/System.out(930): }
05-25 03:48:07.196: I/System.out(930): Array2bd5d455facd7fa2

2 个答案:

答案 0 :(得分:0)

这里的问题是由于数据类型不匹配导致插入查询未执行。数据库中的数据类型最初为TIME,但当更改为TIMESTAMP时,INSERT成功。

答案 1 :(得分:-1)

试试这个

protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub

        //Create the HTTP Post
        HttpParams httpParameters = new BasicHttpParams(); 

        //Setup timeouts -- for later

        HttpClient httpclient = new DefaultHttpClient(httpParameters);
        HttpPost httppost = new HttpPost(webphp);

        try{
     MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

       entity.addPart"id",id);
       entity.addPart("time",time);    
       httppost.setEntity(entity);
        httpclient.execute(httppost);
        } catch (Exception e){
            e.printStackTrace();
        }

        return null;    
    }