我试图从类似的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我正在删除网址中的空格而不保留响应)
答案 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)