Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 每当我尝试执行API时,显示空响应的放心API_Java_Api_Testing_Automation - Fatal编程技术网

Java 每当我尝试执行API时,显示空响应的放心API

Java 每当我尝试执行API时,显示空响应的放心API,java,api,testing,automation,Java,Api,Testing,Automation,下面是代码片段,请给我一些帮助 @Test public void VerifyCountries(){ //========================================================================================================================= //创建Gson生成器对象 Gson gson = new GsonBuilder().create(); Country[]

下面是代码片段,请给我一些帮助

@Test
public void VerifyCountries(){
//========================================================================================================================= //创建Gson生成器对象

    Gson gson = new GsonBuilder().create();
    Country[] country;

    String url = com.Tecksystem_demo_Utility.URL.FixUrl+EndPointURL.country.getResourcePath();

      response= Webservices.Get(url);
==================================================================== Webservices方法

public static Response Get(String uRI){
    RequestSpecification requestSpecification = RestAssured.given();
    requestSpecification.contentType(ContentType.JSON);
    Response response = requestSpecification.get(uRI);
    return response;
}

下面是使用Gson和restassed库完成工作的代码片段。希望这对您所寻找的有帮助:

public class TestClass {

    public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
        Response response = given().
                baseUri("http://services.groupkt.com/state/get/USA/all").get();
        Scanner sc = new Scanner(System.in);
        System.out.println(getLargestCityAndCapital(sc.nextLine(), response.getBody().asString()));
    }

    private static String getLargestCityAndCapital(String searchState, String responseBody) {
        JsonObject resultData = getJsonObjectFromJsonLevelOne("RestResponse", responseBody);
        JsonArray jArray = getJsonArrayFromJsonElement("result", resultData);
        for(int i = 0; i < jArray.size(); i++) {
            JsonObject jobj1 = jArray.get(i).getAsJsonObject();
            String StateName = jobj1.get("name").getAsString();
            String StateAbbr = jobj1.get("abbr").getAsString();
            if(searchState.equalsIgnoreCase(StateName) || searchState.equalsIgnoreCase(StateAbbr)) {
                String largestCity = jobj1.get("largest_city").getAsString();
                String capital = jobj1.get("capital").getAsString();
                return("Largest City is : " + largestCity + " and Capital is : " + capital);
            }
        }
        return ("No such city found.");
    }

    private static JsonObject getJsonObjectFromJsonLevelOne(String jsonElementIdentity, String jsonString) {
        JsonObject jobj = new Gson().fromJson(jsonString, JsonObject.class);
        return (jobj.get(jsonElementIdentity).getAsJsonObject());
    }

    public static JsonArray getJsonArrayFromJsonElement(String jsonElement, JsonObject jsonObject) {
        JsonArray properties = jsonObject.getAsJsonArray(jsonElement);
        return properties;
    }
}
无效情况下:

AL
Largest City is : Birmingham and Capital is : Montgomery
OOPS
No such city found.

你想实现什么,条件是什么,最重要的问题是什么?兄弟,实际上我有一个API ie,问题是我需要从API的get方法获取数据,当我使用字符串url=com.Tecksystem\u demo\u Utility.url.FixUrl+EndPointURL.country.getResourcePath;response=Webservices.Geturl;System.out.printlnresponse.getBody.asString;响应显示为null,并创建一个程序,该程序根据用户输入的州名或州缩写返回最大城市和首都。将在一段时间内返回给您。let res={resresponse:{messages:[Total[249]records found.],结果:[{name:Afghanistan,alpha2_code:AF,alpha3_code:AFG}let search='AFG';let result=res.resresresponse.result.filterres=>{return res.alpha3_code.toLowerCase==search.toLowerCase;};result.mapres=>{console.logres.name;};这些是get Method背后的HTML逻辑非常感谢@learner8269,感谢您为我的任务贡献宝贵的时间