HttpURLclient

HttpURLclient

在子线程中进行网络请求

new Thread(){
        public void run() {
        //查询
            search();};}.start();
String path=URL url=new URL(path);
HttpURLConnection openConnection = (HttpURLConnection) url.openConnection();
openConnection.setReadTimeout(5000);
openConnection.setReadTimeout(5000);
int responseCode = openConnection.getResponseCode();
if(responseCode==200){
            InputStream inputStream = openConnection.getInputStream();
            int len;
            byte[] arr=new byte[1024];
            ByteArrayOutputStream builder=new ByteArrayOutputStream();
            while((len=inputStream.read(arr))!=-1){
                builder.write(arr, 0, len);
            }
            String string = builder.toString();
            Gson gson=new Gson();
            Phone fromJson = gson.fromJson(string, Phone.class);
            String company = fromJson.result.company;
            Message msg=Message.obtain();
            msg.obj=company;
            handler.sendMessage(msg);

Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
String str=(String) msg.obj;

你可能感兴趣的:(其他)