第九章--网络技术的使用

1. HttpURLConnection使用

  • 一个完整使用例子

    private void sendRequestWithHttpUrl() {
           new Thread(new Runnable() {
               @Override
               public void run() {
                   HttpURLConnection  connection=null;
                   BufferedReader  reader=null;
                   try {
                       URL url=new URL("https://www.baidu.com");
                       connection= (HttpURLConnection) url.openConnection();
                       connection.setRequestMethod("GET");
                       connection.setConnectTimeout(8000);
                       connection.setReadTimeout(8000);
                       InputStream inputStream=connection.getInputStream();
                       
                       //对获取到的输入流楷书读取
                       reader=new BufferedReader(new InputStreamReader(inputStream));
                       StringBuilder  response=new StringBuilder();
                       String line;
                       while ((line=reader.readLine())!=null){
                           response.append(line);   
                       }
                       showResponse(line);
                       
                       
                   }catch (Exception  e){
                       e.printStackTrace();
                   }finally {
                       if (reader!=null){
                           try {
                               reader.close();
                           } catch (IOException e) {
                               e.printStackTrace();
                           }
                       }
                       if (connection!=null){
                           connection.disconnect();
                       }
                   }
                   

               }
           }).start();   
        
    }

    private void showResponse(String line) {
        runOnUiThread(new Runnable(){
            @Override
            public void run() {
                 //ui操作,结果显示到界面上
            }
        });
    }
    

2.解析xml方式

  • pull解析
  • SAX解析

3. json解析方式

  • jsonobject 解析
  • gson 解析

4.回调机制开启子线程

    public interface    HttpCallBackListener{
           void onFinish(String response);
           void onFailure(Exception e);
    }
  • 使用
   private void sendRequestWithHttpUrl(String url, final HttpCallBackListener  listener) {
           new Thread(new Runnable() {
               @Override
               public void run() {
                   HttpURLConnection  connection=null;
                   BufferedReader  reader=null;
                   try {
                       URL url=new URL("https://www.baidu.com");
                       connection= (HttpURLConnection) url.openConnection();
                       connection.setRequestMethod("GET");
                       connection.setConnectTimeout(8000);
                       connection.setReadTimeout(8000);
                       InputStream inputStream=connection.getInputStream();

                       //对获取到的输入流楷书读取
                       reader=new BufferedReader(new InputStreamReader(inputStream));
                       StringBuilder  response=new StringBuilder();
                       String line;
                       while ((line=reader.readLine())!=null){
                           response.append(line);
                       }

                       if (listener!=null){
                          listener.onFinish(line);
                       }

                       showResponse(line);


                   }catch (Exception  e){
                       e.printStackTrace();
                       listener.onFailure(e);
                   }finally {
                       if (reader!=null){
                           try {
                               reader.close();
                           } catch (IOException e) {
                               e.printStackTrace();
                           }
                       }
                       if (connection!=null){
                           connection.disconnect();
                       }
                   }


               }
           }).start();

    }
  • 注:不论是使用HttpURLConnection还是okhttp,最终的回调接口还是在子线程中运行。

你可能感兴趣的:(第九章--网络技术的使用)