上传或下载lat long到服务器的最快方法

时间:2016-07-12 03:28:13

标签: android mysql

我需要上传和下载包含纬度,经度和最后一次从mysql-php通过电子邮件过滤的3个字段。 Android中最快和最少占用内存的方法是什么?

在后台尝试异步,但需要大约10秒才能返回。

public class LocationWebService扩展了AsyncTask {

public LocationWebService() {
    // TODO Auto-generated constructor stub
}

@Override
protected Boolean doInBackground(String... arg0) {
    String lat = arg0[0];
    String lng = arg0[1];
    String url = arg0[2];
    String email = arg0[3];
    Log.e("LAT",arg0[0]);
    Log.e("LNG",arg0[1]);
    Log.e("URL",arg0[2]);
    Log.e("EMAIL",arg0[3]);
    InputStream is = null;
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("latitude", lat));
    nameValuePairs.add(new BasicNameValuePair("longitude", lng));
    nameValuePairs.add(new BasicNameValuePair("email", email));
    String result = null;

    try{
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();

        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        result = sb.toString();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

}

1 个答案:

答案 0 :(得分:0)

除非您的网络或服务器出现问题,否则我认为上面的代码不会花费很长时间10秒。