从 iOS 将 JSON 数据发布到 Java Web 服务
Posted
技术标签:
【中文标题】从 iOS 将 JSON 数据发布到 Java Web 服务【英文标题】:Posting JSON data to java web services from iOS 【发布时间】:2014-02-05 08:09:43 【问题描述】:我正在尝试从 ios 应用程序将简单的文本或名称作为 json 发送到我的 java Web 服务,但不幸的是没有运气。这是我的 iOS POST 代码如下:
- (IBAction)cmdRegister:(id)sender
AFSecurityPolicy *policy = [[AFSecurityPolicy alloc] init];
[policy setAllowInvalidCertificates:YES];
AFHTTPRequestOperationManager *operationManager = [AFHTTPRequestOperationManager manager];
[operationManager setSecurityPolicy:policy];
operationManager.requestSerializer = [AFJSONRequestSerializer serializer];
operationManager.responseSerializer = [AFJSONResponseSerializer serializer];
[operationManager POST:@"http://localhost:8080/com.webservice/rest/status/member/"
parameters:@@"name":@"john"
success:^(AFHTTPRequestOperation *operation, id responseObject)
NSLog(@"JSON: %@", [responseObject description]);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
NSLog(@"Error: %@", [error description]);
];
这是我的 java web 服务方法:
@POST
@Path("/member/name")
@Consumes(MediaType.APPLICATION_JSON)
public Response addNewCustomer(@PathParam("name") String name) throws Exception
String returnString=null;
MemberDAO dao = new MemberDAO();
try
ObjectMapper mapper = new ObjectMapper();
Customer customer =mapper.readValue(name, Customer.class);
//add to database the passed in params
int returnCode =dao.insertMembers(name);
//return the success or failure
if(returnCode ==200)
returnString ="Member has been added";
else
return Response.status(500).entity("Unable to process member registration").build();
catch(Exception ex)
ex.printStackTrace();
return Response.ok(returnString).build();
按原样运行没有错误,也没有数据库更新。我已经搜索并找到了类似的帖子,但没有线索请帮忙。
我不确定我做错了什么。
非常感谢任何帮助。
【问题讨论】:
所以你是说你从服务器得到一个返回码 200,但是数据库没有更新?你有没有把两端传输的数据转储看是否正确?? 【参考方案1】:我可以在 iOS 部分为您提供帮助。但我不知道你编写java server(我认为使用JBoss)是否正确。
你给我看代码并试试这个代码(iOS):
- (void)viewDidLoad
[super viewDidLoad];
NSString *strURL = @"http://YOUR_IP:YOUR_PORT/YOUR_PATH/../../..";
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
AFJSONRequestSerializer *serializerRequest = [AFJSONRequestSerializer serializer];
[serializerRequest setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
AFJSONResponseSerializer *serializerResponse = [AFJSONResponseSerializer serializer];
serializerResponse.readingOptions = NSJSONReadingAllowFragments;
serializerResponse.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
manager.requestSerializer = serializerRequest;
manager.responseSerializer = serializerResponse;
UIImage *image = [UIImage imageNamed:@"SIMPLE_IMAGE.png"];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
NSDictionary *parameters = @@"name":@"Jhon";
[manager POST:strURL parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
[formData appendPartWithFormData:imageData name:@"photo"];
success:^(AFHTTPRequestOperation *operation, id responseObject)
strResultResponse = operation.responseString;
failure:^(AFHTTPRequestOperation *operation, NSError *error)
strResultResponse = operation.responseString;
];
您发送到服务器的此代码、参数字符串和 nsdata(image)。你适应你的代码并尝试运行。 对不起,如果没有太大帮助的话。祝你好运。
【讨论】:
谢谢@Ryuchan,我会试一试,让你知道结果。 Web 服务基于 glassfish 4.0,我不确定是否需要配置 glassfish 以接受 POST。我可以轻松地使用 GET 传递参数并返回 JSON 而不会出现问题。如果您知道如何配置 Web 服务器以接受来自客户端的 POST,也请告诉我。【参考方案2】:AFHTTPRequestOperationManager *operationmanager=[AFHTTPRequestOperationManager 管理器];
AFJSONRequestSerializer *serializerRequest = [AFJSONRequestSerializer serializer];
[serializerRequest setStringEncoding:NSUTF8StringEncoding];
operationmanager.requestSerializer = serializerRequest;
operationmanager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"text/html", nil];
[operationmanager POST:getCategoryPath parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
success:^(AFHTTPRequestOperation *operation, id responseObject)
NSLog(@"%@",responseObject);
failure:^(AFHTTPRequestOperation *operation, NSError *error)
];
【讨论】:
以上是关于从 iOS 将 JSON 数据发布到 Java Web 服务的主要内容,如果未能解决你的问题,请参考以下文章
将JSON字符串从IOS Native发送到Javascript(JSON.Parse)时出现Json Parse错误[重复]
将信息从 ios 应用程序保存到 Web 服务器 (POST JSON)
使用 Spring 和 Jackson JSON 将 java.io.Serializable 实例序列化为 JSON [重复]