连接PHP服务器和安卓应用程序


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

我认为这应该有效