【问题标题】:Sending a large String from Android to Servlet using Async Task使用异步任务将大字符串从 Android 发送到 Servlet
【发布时间】:2015-09-20 04:45:15
【问题描述】:

在我的应用程序中,我通过BasicNameValuePairs 向Servlet 发送String,这样:

        HttpClient httpClient = new DefaultHttpClient(); //127.0.0.1 - 10.201.19.153
        HttpPost httpPost = new HttpPost(conn.urls.get("now"));

        List<NameValuePair> nameValuePairs = new ArrayList<>(1);
        nameValuePairs.add(new BasicNameValuePair("order", order));//"tours"
        if(order.equals("reservation")){
            String booking = new Gson().toJson(reservation);
            nameValuePairs.add(new BasicNameValuePair("reservation", booking));
        }

        try {
            // Add name data to request
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            // Execute HTTP Post Request
            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();

            //...
        } //...

除了使用BasicNameValuePairs 之外,还有其他方法可以发送String,或者这是唯一的方法?

【问题讨论】:

标签: java android servlets android-asynctask network-programming


【解决方案1】:

我不完全知道你为什么需要一个替代方案,但它就是 .. 而不是使用 Gson 你可以使用以下代码

{
...
    List<NameValuePair> params = new ArrayList<>();
     params.add(new BasicNameValuePair("string",longString));
     makeHttpRequest(url,"POST", params);
...
}

    public void makeHttpRequest(String url, String method, List<NameValuePair> params) {


        try {
            if (method == "POST"){
                DefaultHttpClient httpClient= new DefaultHttpClient();
                HttpPost httpPost =new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse=httpClient.execute(httpPost);
                HttpEntity httpEntity=httpResponse.getEntity();
                is=httpEntity.getContent();

            }else if (method == "GET"){

                DefaultHttpClient httpClient=new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params,"utf-8");
                if (!paramString.matches(""))
                {
                url +="?"+paramString;
                }
                HttpGet httpGet = new HttpGet(url);
                lru =url;

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity=httpResponse.getEntity();
                is=httpEntity.getContent();

            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
}

希望对你有帮助

【讨论】:

  • 我需要一个替代方案,因为当我使用该代码时,à è 等重音字符无法正确解析......因此我得到了 MalformedJsonException 确实你的代码行:String paramString = URLEncodedUtils.format(params,"utf-8"); 解决这个问题?
  • 此外,我需要使用Post 请求
  • 我仍然不知道为什么这是个问题。 utf-8 编码格式只是将此类特殊字符转换为唯一代码,例如 à 转换为 %C3%A0 ,其中 % 表示“空格”,因此我的代码应该可以正常工作并按预期形成链接
  • 是的,你的代码没问题,但我需要使用post 请求,所以我已经对其进行了调整
  • 要使用 post 方法,您可以使用以下代码 DefaultHttpClien httpClient= new DefaultHttpClient(); String paramString = URLEncodedUtils.format(params,"utf-8"); if (!paramString.matches("")) { url +="?"+paramString; } HttpPost httpPost =new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse httpResponse=httpClient.execute(httpPost); HttpEntity httpEntity=httpResponse.getEntity(); is=httpEntity.getContent();
猜你喜欢
  • 2014-10-11
  • 2012-12-24
  • 1970-01-01
  • 2017-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-21
  • 1970-01-01
相关资源
最近更新 更多