English 中文(简体)
连接和更新来自信使的php服务器
原标题:Connecting and updating php server from Android application

I have created a gps application which works fine. Now what i am trying is to connect it to a php server with mysql database where all the locations of the people are updated automatically.. I am a new user for this part. I have tried some of it but I dont think its write.. Can someone help me and guide me to the process about how should i do it.. Your help will be really appreciated. Below is the code i wrote from a frieds help and the php script.

public class Post extends LocService {{


TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String deviceid = telephonyManager.getDeviceId();



    //this is JSON part to put your information inside it
    String postData = "{"request":{"type":"locationinfo"},"userinfo":{"latitude":""+latitude+"","longitude":""+longitude+"","deviceid":""+deviceid+""}}";


    HttpClient httpClient = new DefaultHttpClient();

    // Post method to send data to server
    HttpPost post = new HttpPost("http://location.net/storeg.php");


    SQLiteDatabase db = databasehelper.getWritableDatabase();
    Cursor cursor = db.query(TABLE, null, null, null, null);

    cursor.moveToFirst();
    while(cursor.isAfterLast() == false) {

        if(cursor.getString(cursor.getColumnIndex("Sync")).equals("yes") ) {

            String mob = cursor.getString(cursor.getColumnIndex("MobileID"));
            String latitude = cursor.getString(cursor.getColumnIndex("Latitude"));
            String longitude = cursor.getString(cursor.getColumnIndex("Longitude"));
            String service = cursor.getString(cursor.getColumnIndex("Service"));


            JSONObject json = new JSONObject();
            try {
                json.put("MobileID", mob);
                json.put("Latitude", latitude);
                json.put("Longitude", longitude);
                json.put("Service", service);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                receive = HttpPostExample.SendJsonUpdate(json, Sync_URL);
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Toast.makeText(context,  receive,Toast.LENGTH_SHORT).show();
        }
        cursor.moveToNext();    
    }
    cursor.close();













    try {
        post.setURI(new URI("http://location.net/storeg.php"));
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // set your post data inside post method    
    try {
        post.setEntity(new StringEntity(postData));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // execute post request here 
    try {
        HttpResponse response = httpClient.execute(post);
    } catch (ClientProtocolException 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","");
if (!$con)
 {
  die( Could not connect:   . mysql_error());
 }

mysql_select_db("mel_db", $con);

$latitude = $_POST[ latitude ];
$longitude = $_POST[ longitude ];
$service = $_POST[ service ];
$devid = $_POST[ devid ];



$sql = "INSERT INTO  `mehul_db`.`locations` (
`id` ,
`devid` ,
`latitude` ,
`longitude` ,
`service`
)
VALUES (
NULL ,   $devid ,   $latitude ,   $longitude ,   $service 
);";
if (!mysql_query($sql,$con))
{
   die( Error:   . mysql_error());
  }

mysql_close($con);

?>

请帮助我推进这一进程...... 或者说我应该怎么做。

* ∗∗∗ 这部法典在改变变量后,是否可将其用于获得纬度和长度。

String result;
    try{


            JSONArray jArray = new JSONArray(result);


            for(int i=0;i<jArray.length();i++){


                    JSONObject json_data = jArray.getJSONObject(i);


                    Log.i("log_tag","id: "+json_data.getInt("id")+


                            ", name: "+json_data.getString("name")+


                            ", sex: "+json_data.getInt("sex")+


                            ", birthyear: "+json_data.getInt("birthyear")


                   );


            }


   finally }


    }catch(JSONException e){


            Log.e("log_tag", "Error parsing data "+e.toString());


    }


}
问题回答
     String url="http://location.net/storeg.php";

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

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
    nameValuePairs.add(new BasicNameValuePair("latitude", latitude));
    nameValuePairs.add(new BasicNameValuePair("longitude", longitude));
    nameValuePairs.add(new BasicNameValuePair("service", service));
    nameValuePairs.add(new BasicNameValuePair("devid", devid));

    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));


    HttpResponse httpResponse = httpClient.execute(httpPost);
    InputStream httpEntity = httpResponse.getEntity().getContent();

您可以利用这一守则通过职位向实验室发送分流器,并对此做出回应。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签