「已注销」 2016-07-05 03:56 采纳率: 0%
浏览 2442

android 怎样给自己写的函数添加一个参数和返回值 方便其他人调用?

写了一个函数 是访问网络的 贴出来 想给自己的函数加一个参数 就是这个请求用的参数input ,和一个返回值就是response.toString();请问要怎么改啊,求指点。。 代码如下

String input = "32&num=00298";

public void urlCon() {

    try {
        URL url = new URL(PATH + input);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(8000);
        connection.setRequestMethod("GET");
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        connection.setDoInput(true);
        OutputStream outputStream = connection.getOutputStream();
        outputStream.flush();
        connection.getResponseCode();

        InputStream in = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
      StringBuilder response = null;
            response  =  new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        System.out.println(response);
        Log.i("返回的数据是 ", response.toString());

    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    } catch (ProtocolException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();

    }

}

  • 写回答

4条回答 默认 最新

  • sinat_26761191 2016-07-05 04:04
    关注

    public String urlCon(String input) {

    try {
        URL url = new URL(PATH + input);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(8000);
        connection.setRequestMethod("GET");
        connection.setUseCaches(false);
        connection.setDoOutput(true);
        connection.setDoInput(true);
        OutputStream outputStream = connection.getOutputStream();
        outputStream.flush();
        connection.getResponseCode();
    
        InputStream in = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
      StringBuilder response = null;
            response  =  new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        System.out.println(response);
        Log.i("返回的数据是 ", response.toString());
                return response.toString();
    
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    } catch (ProtocolException e1) {
        e1.printStackTrace();
    } catch (IOException e1) {
        e1.printStackTrace();
    
    }
    

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘