2013-04-30 45 views
0

我正在製作一個Android客戶端,每隔10秒後將其位置報告給我的服務器。 對於我使用動態DNS的服務器。我必須將緯度,經度和時間戳作爲GET參數發送。 我做了以下工作代碼: package in.kirancity.trackapp;ANDROID:插入日期字符串後,代碼不工作GET URL的日期字符串

import java.io.ByteArrayOutputStream; 
import java.io.IOException; 

import org.apache.http.HttpResponse; 
import org.apache.http.HttpStatus; 
import org.apache.http.StatusLine; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 

import android.app.Activity; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.Menu; 
import android.widget.TextView; 

public class MainActivity extends Activity 
{ 

TextView textLat; 
TextView textLong; 
TextView textRes; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    textLat = (TextView)findViewById(R.id.textLat); 
    textLong = (TextView)findViewById(R.id.textLong); 
    textRes = (TextView)findViewById(R.id.textRes); 

    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    LocationListener ll = new myLocationListener(); 
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 0, ll); 
} 

    class myLocationListener implements LocationListener{ 
     @Override 
     public void onLocationChanged(Location arg0) 
     { 
      if(arg0 != null) 
      { 
       double plong = arg0.getLongitude(); 
       double plat = arg0.getLatitude(); 
       String url = "http://vishalhome.myftp.org/insert.php?la="+Double.toString(plat)+"&ln="+Double.toString(plong)+"&d='Arbitrary'"; 
       textLat.setText(Double.toString(plat)); 
       textLong.setText(Double.toString(plong)); 
       new RequestTask().execute(url); 
      } 
     } 

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

     } 

     @Override 
     public void onProviderEnabled(String arg0) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
      // TODO Auto-generated method stub 

     } 
} 
class RequestTask extends AsyncTask<String, String, String> 
{ 

     @Override 
     protected String doInBackground(String... uri) { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpResponse response; 
      String responseString = null; 
      try { 
       response = httpclient.execute(new HttpGet(uri[0])); 
       StatusLine statusLine = response.getStatusLine(); 
       if(statusLine.getStatusCode() == HttpStatus.SC_OK){ 
        ByteArrayOutputStream out = new ByteArrayOutputStream(); 
        response.getEntity().writeTo(out); 
        out.close(); 
        responseString = out.toString(); 
       } else{ 
        //Closes the connection. 
        response.getEntity().getContent().close(); 
        throw new IOException(statusLine.getReasonPhrase()); 
       } 
      } catch (ClientProtocolException e) { 
       responseString = e.toString(); 
      } catch (IOException e) { 
       responseString = e.toString(); 
      } 
      return responseString; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      TextView tv=(TextView)findViewById(R.id.textRes); 
      tv.setText(result); 
     } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) 
{ 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 

此代碼適用於我。但不是發送實際的時間戳我發送「任意」字符串。 成功執行asyncTask後,插入值並且響應爲:「插入」(由我的php頁面返回)。

但是,當我想這onLocationChanged:

String url = "http://vishalhome.myftp.org/insert.php?la="+Double.toString(plat)+"&ln="+Double.toString(plong)+"&d=\'"; 
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
String currentDateandTime = sdf.format(new Date()); 
url = url + currentDateandTime + "\'"; 

上的位置變化事件的應用程序崩潰。 我試圖把嘗試catch塊,但沒有問題的線索。 我覺得新的RequestTask(url)被調用後問題出來了。 請協助!

回答

0

我想你應該在使用它之前對URL進行編碼。試試這個:

currentDateandTime = URLEncoder.encode(currentDateandTime, "utf-8"); 
url = url + currentDateandTime + "\'"; 

請參閱URL encoding in Android

+0

非常感謝,您的解決方案爲我工作。今天是提交這個完整項目的最後期限。 :) 關於如何在此代碼中獲得高精度位置,您有任何想法嗎?我讀了某處,我需要把enableHighAccuracy =「true」放在某個地方。 – 2013-04-30 04:23:49

+0

歡迎。我想你可以在清單文件中使用。項目祝你好運! – pt2121 2013-04-30 04:28:19

+0

要獲得準確的位置[最佳位置](http://goo.gl/RdYxI)。 [this](http://goo.gl/p6lS9)鏈接可能有助於你。 – user1140237 2013-04-30 04:42:11