网络编程的实践

一个应用程序可能在很多地方要用到网络功能
所以需要把通用的网络操作写到一个公共的类里:
如:

package com.example.networktest;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by yuwei on 2016/10/5.
 */
public class HttpUtil {
    public static String  sendHttpRequest(String address) {
        HttpURLConnection connection = null ;
        try{
            URL url = new URL(address);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(8000);
            connection.setReadTimeout(8000);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            InputStream in = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder response = new StringBuilder();
            String line;
            while ((line = reader.readLine())!=null){
                response.append(line);
            }
            return response.toString();

        }catch (Exception e){
            e.printStackTrace();
            return e.getMessage();
        }finally {
            if(connection!=null){
                connection.disconnect();
            }
        }

    }
}

这时调用时:只需要
String address = “http://www.baidu.com”;
String response = HttpUtil.sendHttpRequest(address);
但是一般网络请求都是耗时操作,这样可能导致,主线程被堵塞



直接在sendHttpRequest()内部开启一个子线程是不行的,因为耗时是在子线程中进行,sendHttpRequest()可能在服务器还没响应的时候就已经执行结束了。


所以解决方案是接口的使用
public interface HttpCallbackListener{
void onFinish(String response);
void onError(Exception e);
}
修改原HttpUtil:

package com.example.networktest;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by yuwei on 2016/10/5.
 */
public class HttpUtil {
    public static String  sendHttpRequest(final String address , final HttpCallbackListener listener) {
       new Thread(new Runnable() {
           @Override
           public void run() {
               HttpURLConnection connection = null ;
               try{
                   URL url = new URL(address);
                   connection = (HttpURLConnection) url.openConnection();
                   connection.setRequestMethod("GET");
                   connection.setConnectTimeout(8000);
                   connection.setReadTimeout(8000);
                   connection.setDoInput(true);
                   connection.setDoOutput(true);
                   InputStream in = connection.getInputStream();
                   BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                   StringBuilder response = new StringBuilder();
                   String line;
                   while ((line = reader.readLine())!=null){
                       response.append(line);
                   }
                  // return response.toString();子线程无法通过return返回数据
                   if (listener!=null){
                       listener.onFinish(response.toString());
                   }

               }catch (Exception e){
                   e.printStackTrace();
                   if (listener!=null){
                       listener.onError(e);
                   }
                 //  return e.getMessage();
               }finally {
                   if(connection!=null){
                       connection.disconnect();
                   }
               }
           }
       }).start();

    }
    public interface  HttpCallbackListener{
        void onFinish(String response);
        void onError(Exception e);
    }
}

这时的调用为:
String address = “http://www.baidu.com”;
String response = HttpUtil.sendHttpRequest(address,new HttpCallbackListener(){
public void onFinish(String response){
根据返回的内容执行具体的逻辑
}

public void oNError(Exception e){
对异常情况进行处理
}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《网络程序设计与实践 网络程序设计与实践 网络程序设计与实践 网络程序设计与实践 》课程是计算机科学与技术专业的重要 》课程是计算机科学与技术专业的重要 》课程是计算机科学与技术专业的重要 》课程是计算机科学与技术专业的重要 》课程是计算机科学与技术专业的重要 》课程是计算机科学与技术专业的重要 》课程是计算机科学与技术专业的重要 》课程是计算机科学与技术专业的重要 实 践类 课程之一。随着计算机网络技术的迅速发展和在当今信息社会中 课程之一。随着计算机网络技术的迅速发展和在当今信息社会中 课程之一。随着计算机网络技术的迅速发展和在当今信息社会中 课程之一。随着计算机网络技术的迅速发展和在当今信息社会中 课程之一。随着计算机网络技术的迅速发展和在当今信息社会中 课程之一。随着计算机网络技术的迅速发展和在当今信息社会中 课程之一。随着计算机网络技术的迅速发展和在当今信息社会中 课程之一。随着计算机网络技术的迅速发展和在当今信息社会中 课程之一。随着计算机网络技术的迅速发展和在当今信息社会中 课程之一。随着计算机网络技术的迅速发展和在当今信息社会中 课程之一。随着计算机网络技术的迅速发展和在当今信息社会中 的广泛应用,给《 的广泛应用,给《 的广泛应用,给《 的广泛应用,给《 网络程序设计与实践 网络程序设计与实践 网络程序设计与实践 网络程序设计与实践 》课程的教学提出了新更高 》课程的教学提出了新更高 》课程的教学提出了新更高 》课程的教学提出了新更高 》课程的教学提出了新更高 》课程的教学提出了新更高 的要求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值