gpt4 book ai didi

swift - 发布请求在 postman 中工作正常,但我无法使其与 Swift 4 和 Alamofire 一起工作

转载 作者:行者123 更新时间:2023-11-30 11:48:55 24 4
gpt4 key购买 nike

我正在开发一个 Swift 4 应用程序,它与使用 JDBCDB2 数据库通信的 JAVA DAO 进行通信 。我正在使用 JAX-RS 来提供 Web 服务功能。我还使用 Alamofire 4 库作为应用程序的一部分。

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

错误

[glassfish 4.1] [SEVERE] [] [app.log] [tid: _ThreadID=31 _ThreadName=http-listener-1(4)] [timeMillis: 1517241179331] [levelValue: 1000] [[ Exception : 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

最佳答案

您正在向请求方法传递参数而不是参数。这只是您提交问题时的拼写错误还是您有参数变量?我想如果您不这样做,Xcode 会让您知道。

关于swift - 发布请求在 postman 中工作正常,但我无法使其与 Swift 4 和 Alamofire 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48528457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com