android从url获取带有空格的响应

时间:2014-03-12 05:23:57

标签: android url whitespace httpresponse

我试图从类似的URL获取响应(包含空格),但总是删除空格。这是我的代码

HttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(link); 
            HttpResponse httpResponse;
            httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();       

            String  s = EntityUtils.toString(httpEntity, HTTP.UTF_8);

            String[] ar = s.split(" ");
            Log.e("responsesize" , String.valueOf(ar.length));
            for(int i =0 ; i < ar.length ; ++i)
            {
                Log.e("response" , ar[i]);
            }

我的回答是:TRYING2020/1/12014/04/1103但它应该是:

TRYING 2020/1/1 2014/04/11 0    3

任何想法如何做到这一点? (如果这是愚蠢但是试图在其上找到一个帖子,但是evthg我正在删除网址中的空格而不保留响应)

2 个答案:

答案 0 :(得分:0)

试试这种方式

try{

                StringBuffer sb = new StringBuffer();
                URL url = new URL(location);
                urlConnection = (HttpURLConnection) url.openConnection();

                BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line + "\n");
                }
                return sb.toString();
            }  
    finally {
                urlConnection.disconnect();
            }

答案 1 :(得分:0)

找到了它。实际上我认为一个标签是4个空格的组合...所以如果我用空格分割我得到一个数组。但是用标签分割给我回复