使用java通过josnws api更新liferay用户并获取NullPointerException

时间:2017-10-23 11:25:26

标签: java json liferay liferay-6

我尝试使用另一个Liferay门户(B)中的liferay实例(A)中的 api / jsonws / user / update-user / 更新用户,这两个门户具有相同的版本6.2。问题是当我通过这种方法从B向A发送请求时

public String updateUserIportal(String url,String loginUser, String 
    loginPwd,long userId, String screenName,String email,String firstName, 
    String middleName, String lastName,boolean male,String DOB, String 
    reminderQueryQuestion, String reminderQueryAnswer, String greeting,String 
    comment) throws  IOException, PortalException, SystemException{
    //String url = "http://localhost:8080/api/jsonws/user/update-user/";
    String birthDate;
    String birthMounth;
    String birthYear;
    //DOB format: dd/MM/yyyy
    birthDate = DOB.substring(0,2);
    birthMounth = DOB.substring(3,5);
    int birthM = Integer.parseInt(birthMounth)-1;
    birthYear = DOB.substring(6);   
    String parameters="";
  /*
   http://localhost:8080/api/jsonws/user/
   update-user/
   user-id/24007/
   -old-password/
   -new-password1/
   -new-password2/
   password-reset/false/
   -reminder-query-question/
   -reminder-query-answer/
   screen-name/newwwwwwwwww/
   email-address/lienthong23%40gmail.com/
   facebook-id/0/
   -open-id/
   language-id/vi_VN/
   time-zone-id/UTC/
   -greeting/
   comments/T%E1%BA%A1o%20b%C4%83ng%20khung%20li%C3%AAn%20th%C3%B4ng/
   first-name/Tesst/
   middle-name/li%C3%AAn%20th%C3%B4ng/
   last-name/m/
   * prefix-id/0/
   * suffix-id/0/
   * male/true/
   * birthday-month/8/
   * birthday-day/02/
   * birthday-year/1993/
   * -sms-sn/
   * -aim-sn/
   * -facebook-sn/
   * -icq-sn/
   * -jabber-sn/
   * -msn-sn/
   * -my-space-sn/
   * -skype-sn/
   * -twitter-sn/
   * -ym-sn/
   -job-title/
   -group-ids/
   -organization-ids/
    -role-ids/-user-group-roles
    /-user-group-ids
    */
    parameters +="user-id/"+userId+"/-old-password/-new-password1/"
    +"-new-password2/password-reset/false/-reminder-query-question/"
    +"-reminder-query-answer/screen-name/"+screenName+"/email-address/"
    +email.replace("@", "%40") + "/" + "facebook-id/0/-open-id/"
    +"language-id/vi_VN/time-zone-id/UTC/-greeting/comments/Egov_portal/"
    +"first-name/"
    +URLEncoder.encode(firstName,StandardCharsets.UTF_8.toString())
    +"/middle-name/"
    +URLEncoder.encode(middleName,StandardCharsets.UTF_8.toString())+"/"
    +"last-name/"
    +URLEncoder.encode(lastName,StandardCharsets.UTF_8.toString())
    +"/prefix-id/0/"+"suffix-id/0/male/"+Boolean.toString(male)
    +"/birthday-month/"+birthM+"/birthday-day/"+birthDate
    +"/birthday-year/"+birthYear+"/-sms-sn/-aim-sn/-facebook-sn/-icq-sn/"
    +"-jabber-sn/-msn-sn/-my-space-sn/-skype-sn/-twitter-sn/-ym-sn/"
    +"-job-title/-group-ids/-organization-ids/-role-ids/-user-group-roles/"
    +"-user-group-ids";

    URL obj = new URL(url+parameters);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");
    con.setRequestProperty("User-Agent", "Mozilla/5.0");
    con.setRequestProperty("Accept-Language", "en-US,en;q=0.5,utf-8");
    con.setDoOutput(true);
    byte[] message = (loginUser+":"+loginPwd).getBytes("UTF-8");
    String encoded = 
    javax.xml.bind.DatatypeConverter.printBase64Binary(message);
    con.setRequestProperty("Authorization", "Basic "+encoded);
    int responseCode = con.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Post parameters : " + parameters);
    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 && (in.readLine() != "")) {
        response.append(inputLine);
    }
    in.close();

    // print result
    System.out.println("response String:");
    System.out.println(response.toString().trim());
    String jsonString =response.toString().trim();
        if(jsonString.indexOf("Exception")>-1){
                return "ERROR";
        } else{
            if(jsonString.contains("emailAddress")
             &&jsonString.contains("companyId")){
                return "OK";
            } else{
                return jsonString;
        }

    }
}

毕竟, 门户网站A返回java.lang.NullPointerException,也许我丢失了 serviceContext obj。我如何通过URL发送serviceContext obj,

0 个答案:

没有答案