邮寄请求在邮递员中运行正常,但我无法使其与Swift 4和Alamofire

时间:2018-01-30 18:45:29

标签: swift alamofire

我正在开发一个 Swift 4 应用程序,该应用程序与使用 JDBC < JDBC 数据库与 DB2 数据库进行通信的 JAVA DAO 进行对话/ strong>即可。我正在使用 JAX-RS 来提供Web服务功能。我也使用 Alamofire 4 库作为应用程序的一部分。

当我使用POST请求调用DAO时,该请求在Postman中传递了一系列内容类型为x-www-URLEncoded的参数,调用成功完成,结果以200 msg传回,但是当我使用Alamofire请求在Swift 4中尝试它时,JAVA DAO在DAO的结果集上抛出错误。

错误

  

[glassfish 4.1] [SEVERE] [] [app.log] [tid:_ThreadID = 31   _ThreadName = http-listener-1(4)] [timeMillis:1517241179331] [levelValue:1000] [[例外:   java.lang.NullPointerExceptioncom.sun.gjc.spi.base.ResultSetWrapper.next(ResultSetWrapper.java:103)

Java code

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.TEXT_PLAIN)


public int insertUserProfile(
      @FormParam("password") String password,         
      @FormParam("fName") String fName,
      @FormParam("city") String city,
      @FormParam("stateProv") String stateProv,
      @FormParam("zip") String zip,
      @FormParam("emailId") String emailId,
      @FormParam("age") short age,
      @FormParam("sex") String sex,
      @FormParam("uType") String uType,
      @FormParam("salary") BigDecimal salary,
      @FormParam("occupation") String occupation,
      @FormParam("secQuestion") String secQuestion,
      @FormParam("secAnswer") String secAnswer
         ) throws Exception{


 try {

   String proc = "CALL USERINFO(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
   int whichquery = 1;
   int cols = 1;


   CallableStatement stmt = conn.prepareCall(proc);

   stmt.setInt(1,whichquery);
   stmt.setInt(2,0);
   stmt.setString(3,password);
   stmt.setString(4,fName);
   stmt.setString(5,city);
   stmt.setString(6,stateProv);
   stmt.setString(7,zip);
   stmt.setString(8,emailId);
   stmt.setInt(9,age);
   stmt.setString(10,sex);
   stmt.setString(11,uType);
   stmt.setBigDecimal(12,salary);
   stmt.setString(13,occupation);
   stmt.setString(14,secQuestion);
   stmt.setString(15,secAnswer);
   stmt.registerOutParameter(16,Types.VARCHAR, sqlState);
   stmt.registerOutParameter(17,Types.INTEGER, sqlCode);
   stmt.registerOutParameter(18,Types.VARCHAR, sqlMsg);
  if (debug_sw) { System.out.println("***** setString *****"); }




   rs = stmt.executeQuery();

   if (debug_sw) { System.out.println("***** executeQuery *****"); }

   if (debug_sw) { System.out.println("***** Initialized list *****"); }

   if (sqlCode == 0 && rs.next() )
   {
     total_rows = rs.getMetaData().getColumnCount();
   }



  rs.close();
  stmt.close();
  conn.close();

  if (debug_sw) { System.out.println("***** Closed rs, stmt, & conn *****"); }

  if (debug_sw) { System.out.println("***** Returning JSONArray *****"); }

  return total_rows;

Swift 4 / Alamofire代码

  let parameters: Parameters = [
                        "password":passW,
                        "fName":fname,
                        "city":cityname,
                        "stateProv":statename,
                        "zip": zipname,
                        "emailId": emailname,
                        "age": agename,
                        "sex": sexname,
                        "uType":userTypeName,
                        "salary":salaryAmount,
                        "occupation": occupationname,
                        "secQuestion": secretQuestion,
                        "secAnswer": secretAnswer
                        ]
                      Alamofire.request(url, method:.post, 
                      parameters:parameter, encoding:URLEncoding.default)
                      .reponse{response in
                       print("The output is: ",response, request!.description)
                       }

PostMan代码

      POST /dao/webapi/myprofile HTTP/1.1
      Host: localhost:8080
      Content-Type: application/x-www-form-urlencoded
      Cache-Control: no-cache
      Postman-Token: 7681b040-1a3bo-1k0k-21b8-c7993b32ab28

        password=dusty962!&fName=george&city=glassfish&
        stateProv=NJ&zip=09023&emailId=douglas%40test.com&
        age=1960&sex=male&uType=U&salary=6&occupation=programmer
        &secQuestion=married&secAnswer=twice  

1 个答案:

答案 0 :(得分:0)

您正在将参数而不是参数传递给请求方法。这只是你问题提交中的拼写错误还是你有参数变量?如果你没有,我认为Xcode会通知你。