0

我试图连接我的应用程序来获取用户的位置。现在我希望它使用 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部分出错。请帮我解决这个问题,代码会发布纬度和经度。

4

1 回答 1

0

从这些行中删除“+”号

    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));

我认为这应该有效

于 2012-05-16T05:03:38.323 回答