我想要做的是从一个 url 生成一个字节数组。
byte[] data = WebServiceClient.download(url);
url
返回json
public static byte[] download(String url) {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
try {
HttpResponse response = client.execute(get);
StatusLine status = response.getStatusLine();
int code = status.getStatusCode();
switch (code) {
case 200:
StringBuffer sb = new StringBuffer();
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
is.close();
sContent = sb.toString();
break;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return sContent.getBytes();
}
这data
被用作参数String
String json = new String(data, "UTF-8");
JSONObject obj = new JSONObject(json);
出于某种原因,我收到此错误
I/global ( 631): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
sContent = sb.toString();
我认为这里或这里一定缺少某些东西,return sContent.getBytes();
但我不确定。