2012-04-30 27 views
2

長い間、私は本当に欲しいもののための解決策を得ることができなかった多くの研究の後であった。私はGPS追跡アプリケーションを作った。今私は私のローカルホストサーバーに電話から得る座標を投稿したい。そして、それも5分ごとに座標を掲示します。私はWAMPを使ってサーバーを作った。アンドロイドの携帯電話からサーバーに投稿

PHPスクリプト

?php 

echo 'Hello, world!'; 

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

mysql_select_db("ric_db", $con); 

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





$sql = "INSERT INTO `ric_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); 

?> 

Javaファイル

@Override 

public void onCreate(Bundle savedInstanceState) 

{ 

super.onCreate(savedInstanceState); 

setContentView(R.layout.main); 



/* Use the LocationManager class to obtain GPS locations */ 

LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

LocationListener mlocListener = new MyLocationListener(); 



mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 



} 



/* Class My Location Listener */ 

public class MyLocationListener implements LocationListener 

{ 
Context context; 
public void onLocationChanged(Location loc) 

{ 
StringBuilder sb = null; 
    String result = null; 
    InputStream is = null; 

double latitude = loc.getLatitude(); 

    double longitude = loc.getLongitude(); 
    Toast.makeText(context, "Latitude:" +latitude + "Longitude:" +longitude, 5000).show(); 
    TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE); 
    String Devid = telephonyManager.getDeviceId(); 




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

String url="http://localhost/mehul/store.php"; 


List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3); 
nameValuePairs.add(new BasicNameValuePair("Devid", Devid)); 
nameValuePairs.add(new BasicNameValuePair("latitude", Double.toString(latitude))); 
nameValuePairs.add(new BasicNameValuePair("longitude", Double.toString(longitude))); 

HttpClient httpclient = new DefaultHttpClient(); 

HttpPost httppost = new HttpPost(url); 

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

HttpResponse response = httpclient.execute(httppost); 

HttpEntity http_entity = response.getEntity(); 

Log.i("postData", response.getStatusLine().toString()); 
BufferedReader br = new BufferedReader(
new InputStreamReader(http_entity.getContent())); 
String res = br.readLine(); 
System.out.println(res); 


} 





public void onProviderDisabled(String arg0) { 
// TODO Auto-generated method stub 

} 
public void onProviderEnabled(String provider) { 
// TODO Auto-generated method stub 

} 
public void onStatusChanged(String provider, int status, Bundle extras) { 
// TODO Auto-generated method stub 

} 

は、いくつかは

+0

コメントしてください。私はあなたがサーバー上のmysqlデータベースにデータを取得しようとしているが、実行しているエラーや何が問題になっているのかは述べていません。 – DeaconDesperado

+0

また、データ交換フォーマット(json/xml)を使用しているのか、リクエストに埋め込まれた投稿/取得フィールドとして送信するだけですか? – DeaconDesperado

+1

投稿に質問はありません。具体的な教授の質問をしてください。 – dm03514

答えて

0

ではなく、サーバーのIPアドレスを使用して、あなたのURLでlocalhostを使用しないでください...これで私を助けてください。 そして、あなたのPHPスクリプトは悪い開始タグがあります。?phpの代わり<?php

詳細を記入してくださいどのようなエラーがこれで解決しない場合は、あなたが得るん...

+0

どのようにサーバーのIPアドレスを取得する...?ウィンドウ内の –

+0

:コマンドプロンプトを開き( 'run> cmd')、' ipconfig'とタイプするとあなたのIPアドレスが得られます。 Ubuntuの :ネットワークアプレットを右クリックし、「接続情報」を選択します。 – vtuhtan

関連する問題