从服务器获取json

String apiUrl=
"https://www.kuaidi100.com/query?type=shentong&postid=227728570456";
URL url=new URL(apiUrl);
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
InputStream is=conn.getInputStream();
byte []temp=new byte[1024];
int length=0;
ByteArrayOutputStream baos=new ByteArrayOutputStream();
while ((length=is.read(temp))!=-1){
    baos.write(temp,0,length);
}
json=baos.toString();
Message msg=new Message();
msg.what=1;
msg.obj=json;
handler.sendMessage(msg);
记得注册联网权限

你可能感兴趣的:(从服务器获取json)