如何在函数中包含代码

时间:2018-07-27 08:02:10

标签: 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);
           }

    }

}

1 个答案:

答案 0 :(得分:0)

创建一个采用String的方法,看来您需要StringBuffer响应...

public StringBuffer doSomething(String url){
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    int responseCode = con.getResponseCode();
    //etc
    return response;
}

只需将两个URL从主要传递给它:

String url = "https://tfs.tpsonline...
StringBuffer response = doSomething(url);