Tôi đang làm điều gì đó tương tự với chính mình nhưng tôi đang sử dụng máy chủ. Lý do tôi làm như vậy là vì lý do tính toán. Khi ứng dụng đầu tiên bắt đầu một yêu cầu HTTP được gửi đến một tập lệnh php có chứa một truy vấn MySQL. Kịch bản lệnh php thu thập các lat/long hiện tại của người dùng và trả về một JSON tất cả các điểm trong phạm vi 1KM của vị trí hiện tại. Sau đó, ứng dụng phân tích cú pháp JSON đó thành một cơ sở dữ liệu MySQL cục bộ và thực hiện các công cụ với nó.
Để gửi yêu cầu HTTP và phân tích cú pháp JSON thành một cơ sở dữ liệu tôi đang làm như sau ..
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://URL");
httppost.setEntity(new UrlEncodedFormEntity(parameters));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
//convert response to string
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
new AlertDialog.Builder(this)
.setTitle("Woops!")
.setMessage("Getlocations:It appears the server is not reachable. Check your connectivity and try again.")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
//User wants to start activity..
}
})
.show();
Log.e("log_tag", "Error in connection or convert "+e.toString());
}
//parse json data
try{
System.out.println("parsing data");
JSONArray jArray = new JSONArray(result);
if(icount != 0){
try{
SQLiteDatabase db = tweets.getWritableDatabase();
db.delete(TABLE_NAME,null,null);
}finally {
tweets.close();
}
}
//TODO: Change this to num
for(int i=0;i<jArray.length() && q < 1;i++){
System.out.println("ARRAY LENGTH " + jArray.length());
q++;
JSONObject json_data = jArray.getJSONObject(i);
System.out.println("Getting Info");
Double lat = json_data.getDouble("lat");
Double lng = json_data.getDouble("lng");
String link = json_data.getString("link");
String message = json_data.getString("message");
String sname = json_data.getString("sname");
String name = json_data.getString("name");
//Add each entry to SQL database...
System.out.println("Adding table row!");
try{
addloc(name,sname,message,link,lat,lng);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
tweets.close();
}
count();
}
}catch(JSONException e){
new AlertDialog.Builder(this)
.setTitle("Woops!")
.setMessage("Getlocations: It appears something went wrong processing the information.")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
//User wants to start activity..
}
})
.show();
Log.e("log_tag", "Error parsing data "+e.toString());
}
Để làm việc này tất cả bạn cần là một tập tin php ngắn mà mất Lat/lng Something như ...
<?php
mysql_connect("host","user","pass");
mysql_select_db("database");
$lat = $_REQUEST['currlat'];
$lng = $_REQUEST['currlng'];
$q=mysql_query("QUERY") or die(mysql_error());
if (mysql_num_rows($q) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
if(!q){
echo "NO RESULT";
exit;
}
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
?>
Điều đó sẽ đưa vào giá trị và in kết quả bằng JSON.
// chỉnh sửa: Để nhận được lat/lng ở địa điểm đầu tiên, tôi đang sử dụng GPS.
Hy vọng điều này sẽ hữu ích.
Cảm ơn bạn đã gợi ý Twaroog - nó hoạt động khá tốt. Tôi đã có thể di chuyển xa hơn với dự án của tôi một chút nhờ bạn. – Pavel
Không vấn đề gì, tôi cũng làm như vậy trong dự án của mình. – LordTwaroog