用户对象无法解析为变量

时间:2016-04-19 14:38:47

标签: java

新手。刚刚写下了一个返回User对象的登录函数,但是在最后一行收到错误后返回oUser为“oUser无法解析为变量”。有人可以帮助我。我使用User作为sLoginContractTest函数的返回类型。

    public User sLoginContractTest(String sUserName, String sPassword, String sDomain,
            String sBusUnitID) throws Exception {

            String[] sInfo = new String[2];


            Gson gson = new GsonBuilder().create();

            // Create your http client
            CloseableHttpClient httpclient = HttpClientBuilder.create().build();

            // Create http post object
            // HttpPost postRequest = new HttpPost(sUrl);
            HttpPut putRequest = new HttpPut(this.sAuthUrl);

        // Message Body
            StringEntity input = new StringEntity("{\"userId\":\"" + sUserName
                + "\",\"password\":\"" + sPassword + "\",\"businessUnitId\":\""
                + sBusUnitID + "\",\"domainName\":\"" + sDomain + "\"}");

        // Set content type for post
        input.setContentType("application/json");

        // attach message body to request
        putRequest.setEntity(input);

        // submit request and save response
        HttpResponse response = httpclient.execute(putRequest);

        // get status code from response
        int sStatusCode = response.getStatusLine().getStatusCode();

        // Check if status code returned is 200 (success) or other (failure)
        if (sStatusCode != 200) {
            ReportResults("FAIL", "Login Failed due to " + response.toString(),
                    false);
            Assert.fail(response.getStatusLine().getReasonPhrase());

            sInfo = null;
        }   
        // Expected response
        else if (sStatusCode == 200) {  
             System.out.println(response.toString());
             ReportResults("PASS", "Logged in Successfully.", false);

             // Get response body (entity and parse to string
             String sEntity = EntityUtils.toString(response.getEntity());
             User oUser = gson.fromJson(sEntity, User.class);

             // Get securityID and token
             sInfo[0] = this.getJsonResponseValue(sEntity, "securityId");
             sInfo[1] = this.getJsonResponseValue(sEntity, "token");

             //Check to see if Contract is valid

             try{

                 if(oUser!=null){
                     return oUser;
                 }
                 else{
                     return null;
                 }
             }
                 catch(Exception e){
                //If exception, contract failed (might want to check for specific exception here)
                  ReportResults("FAIL", "Login user contract failed because: " + e.getMessage(),false);
                 } 
                 this.setUserID(sUserName);
                    this.sAuthToken = sInfo;
                    System.out.println("the security id is" + this.sAuthToken[0]);
                    System.out.println("the token id is " + this.sAuthToken[1]);


                }

            return oUser;   

}

0 个答案:

没有答案