如何将LatLng添加到NameValuePairs

时间:2014-04-26 19:37:29

标签: android

*我创建了asyntask,将一些细节发送到我的网页并处理它 *我已成功将字符串值发送到我的页面
*但现在问题是我想发送变量 markerPosition 这是 LatLng 的类型
* Eclipse建议我将其更改为String,但我知道它不是解决方案 *有没有其他方式发送..?

private class sendToWebsite extends AsyncTask<Void, String, String> {

    String reverseString;
    String username,Place,Status;
    LatLng markerPosition;

    public sendToWebsite(String userName, String place_text,
            String status_text, LatLng mPosition) {

        username = userName;
        Place = place_text;
        Status = status_text;
        markerPosition = mPosition;

    }

    @Override
    protected String doInBackground(Void... params) {

        String URL = "url here";

        try {
            HttpClient httpclient = new DefaultHttpClient();

            HttpPost httppost = new HttpPost(URL);

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                    1);
            nameValuePairs
                    .add(new BasicNameValuePair("username", userName));
            nameValuePairs.add(new BasicNameValuePair("place", Place));
            nameValuePairs.add(new BasicNameValuePair("status", Status));
            nameValuePairs.add(new BasicNameValuePair("position", markerPosition));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            ResponseHandler<String> responseHandler = new BasicResponseHandler();

            String response = httpclient.execute(httppost, responseHandler);

            reverseString = response;

        } catch (UnsupportedEncodingException e1) {

            e1.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }

        return reverseString;
    }

    @Override
    protected void onPostExecute(String result) {

        super.onPostExecute(result);

        String flag = "success";
        if (result.equals(flag)) {
            Toast.makeText(getBaseContext(), "Success", Toast.LENGTH_LONG)
                    .show();
        }

    }

}

2 个答案:

答案 0 :(得分:1)

LatLng是一个不能简单地打包并在Http请求中发送的对象。

您需要从LatLng中提取纬度和经度,以便通过Http请求将其作为字符串发送。例如:

nameValuePairs.add(new BasicNameValuePair("latitude", Double.toString(markerPosition.latitude)));
nameValuePairs.add(new BasicNameValuePair("longitude", Double.toString(markerPosition.longitude)));

在你的后端,将收到的值转换回双打并处理它们。

答案 1 :(得分:1)

删除位置值,然后在您的网站名称纬度和经度中创建一个新参数,然后将标记的纬度和经度添加到nameValuePairs

删除:

nameValuePairs.add(new BasicNameValuePair("position", markerPosition));

添加一对新:

nameValuePairs.add(new BasicNameValuePair("latitude", markerPosition.latitude + ""));
nameValuePairs.add(new BasicNameValuePair("longitude", markerPosition.longitude +""));