[英]Connecting php server and android app
我正在嘗試連接我的應用程序以獲取用戶位置。現在我希望它使用mysql數據庫在php服務器上對其進行更新。 它在代碼的某些部分給出了錯誤
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.site.net/storeg.php");
}
InputStream is;
String result = "";
//the year data to send
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("deviceid", +deviceid));
nameValuePairs.add(new BasicNameValuePair("latitude", +latitude));
nameValuePairs.add(new BasicNameValuePair("longitude", +longitude));
{
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://location.site.net/storeg.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
}
//convert response to string
try{
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");
Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","deviceid: "+json_data.getInt("deviceid")+
", latitude: "+json_data.getString("latitude")+
", longitude: "+json_data.getInt("longitude"));
Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
}
}
}
.add部分出錯。.請幫助我,此代碼將發布緯度和經度。
從這些行中刪除“ +”號
nameValuePairs.add(new BasicNameValuePair("deviceid", +deviceid));
nameValuePairs.add(new BasicNameValuePair("latitude", +latitude));
nameValuePairs.add(new BasicNameValuePair("longitude", +longitude));
使他們成為
nameValuePairs.add(new BasicNameValuePair("deviceid", deviceid));
nameValuePairs.add(new BasicNameValuePair("latitude", latitude));
nameValuePairs.add(new BasicNameValuePair("longitude", longitude));
我認為這應該工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.