java - 如何将代码包含在函数中

标签 java json function api oop

这是我的java代码,我对url和url1重复相同的步骤,所以我想创建一个函数,在其中单独放置我的url代码和单独的url1代码,然后在主类中调用它。首先我想访问 String url,然后我想访问 String url1。因为我是 java 新手,所以我是 java 新手,所以我无法将其包含到函数中

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONArray;
import org.json.JSONObject;


public class Test_URL_Req {

    public static void main(String[] args){
        // TODO Auto-generated method stub
        try {  
        String id ="301";
        String url = "https://tfs.tpsonline.com/IRIS%204.0%20Collection/Main/_apis/build/definitions?api-version=4.1";
           String url1 ="https://tfs.tpsonline.com/IRIS%204.0%20Collection/Main/_apis/build/builds?api-version=4.1&definitions=" + id +"&resultFilter=succeeded&$top=1";
         URL obj = new URL(url);
         URL obj1 = new URL(url1);
         HttpURLConnection con = (HttpURLConnection) obj.openConnection();
         HttpURLConnection con1 = (HttpURLConnection) obj1.openConnection();
         int responseCode = con.getResponseCode();
         int responseCode1 = con1.getResponseCode();
         System.out.println("\nSending 'GET' request to URL : " + url);
         System.out.println("Response Code : " + responseCode);
         System.out.println("\nSending 'GET' request to URL : " + url1);
         System.out.println("Response Code : " + responseCode1);
         BufferedReader in = new BufferedReader(
                 new InputStreamReader(con.getInputStream()));
         BufferedReader in1 = new BufferedReader(
                 new InputStreamReader(con1.getInputStream()));
         String inputLine;
         String inputLine1;
         StringBuffer response = new StringBuffer();
         while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
            //System.out.println(response);
         }
         StringBuffer response1 = new StringBuffer();
         while ((inputLine1 = in1.readLine()) != null) {
            response1.append(inputLine1);
            //System.out.println(response1);
         }
         in.close();

         JSONObject obj_JSONObject = new JSONObject(response.toString());
         JSONObject obj_JSONObject1 = new JSONObject(response1.toString());
         JSONArray obj_JSONArray = obj_JSONObject.getJSONArray("value");
         JSONArray obj_JSONArray1 = obj_JSONObject1.getJSONArray("value");
        for(int i=0; i<obj_JSONArray.length();i++)
        {
            JSONObject obj_JSONObject2 = obj_JSONArray.getJSONObject(i);

            String value = obj_JSONObject2.getString("name");
            //String value = obj_JSONObject2.get("id").toString();
            //System.out.println(value);

            String toSearch= "DEVOPS";
           if(value.equals(toSearch)){
                 System.out.println("STATUS:-");
                 System.out.println(value);
                 String result =obj_JSONObject2.getString("name");
                 System.out.println("BUILD NAME");
                 System.out.println(result);
                 String Def_id = obj_JSONObject2.get("id").toString();
                 System.out.println("DEFINATION ID");
                 System.out.println(Def_id);

                 break;

            }

        }

        for(int i=0; i<obj_JSONArray1.length();i++)
        {
            JSONObject obj_JSONObject2 = obj_JSONArray1.getJSONObject(i);

            String value = obj_JSONObject2.getString("result");
            //String value = obj_JSONObject2.get("id").toString();
            //System.out.println(value);

            String toSearch1= "succeeded";
           if(value.equals(toSearch1)){
               System.out.println("#######################################");
                 System.out.println("RESULT");
                 System.out.println(value);

                 String result =obj_JSONObject2.getString("status");
                System.out.println("STATUS");
                 System.out.println(result);
                 String Def_id = obj_JSONObject2.get("id").toString();
                 System.out.println("BUILD ID");
                 System.out.println(Def_id);

                 boolean  keepForever =obj_JSONObject2.getBoolean("keepForever");

                if(keepForever == false)
                {
                    keepForever=true;
                }

                 System.out.println(keepForever);

            }

        }

    } catch (Exception e) {
             System.out.println(e);
           }

    }

}

最佳答案

 public static String getURLResponse( String url){
      try {
          System.out.println("\nSending 'GET' request to URL : " + url);
          URL obj = new URL(url);
          HttpURLConnection con = (HttpURLConnection) obj.openConnection();
          int responseCode = con.getResponseCode();

          System.out.println("Response Code : " + responseCode);
          BufferedReader in = new BufferedReader(
                  new InputStreamReader(con.getInputStream()));

          String inputLine;
          StringBuffer response = new StringBuffer();
          while ((inputLine = in.readLine()) != null) {
              response.append(inputLine);
              //System.out.println(response);
          }
          in.close();
        return  response.toString();
      } catch (Exception e) {
          System.out.println(e);
      }
      return null;
  }

在主方法中 -

public static void main(String[] args){
            String url = "....";
            String url1 =".....";

            String response  = getURLResponse(url);
            String response1  = getURLResponse(url1);

            JSONObject obj_JSONObject = new JSONObject (response);
            JSONObject obj_JSONObject1 = new JSONObject(response1);
...
}

关于java - 如何将代码包含在函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51553452/

相关文章:

c# - 使用带有 JsonPath 的 SelectTokens 的问题/错误

javascript - 正则表达式来匹配 javascript 函数的定义并确保它返回一些东西?

javascript - 箭头函数不起作用。普通的可以。为什么?

Javascript 代码块在返回被调用函数的结果之前完成?我缺少什么?

java - Java中的数组一维右旋

javascript - 使用嵌套映射的 JSON 对象反序列化,Java Spring

php - 在 Doctrine 结果上使用 jsonModel 返回 JSON 数据时得到空响应 - 特殊字符编码问题

java - 将现有 Maven 项目迁移到 OSGI Bundle

java - JList 未使用自定义 ListModel 进行更新

javascript - 根据另一个下拉列表填充一个下拉列表并获取 "value"而不是下拉列表显示值