iOS中的Post调用以及JAVA中的Webservice实现

iOS中的Post调用以及JAVA中的Webservice实现,java,ios,web-services,post,Java,Ios,Web Services,Post,我被这个问题困住了。希望你能帮忙 我已经在Java中实现了webservice,并使用GET方法在iOS中使用了webservice,没有任何问题。但是,我一直无法使POST方法起作用 是否有任何方法可以检查服务器实现是否正确?在浏览器中输入URL不会调用POST方法。下面的方法可能有什么错误 这是我的服务器实现: @Path("/v1/consumer/") public class V1_consumer { @POST @Path("/updateConsumptionMetric")

我被这个问题困住了。希望你能帮忙

我已经在Java中实现了webservice,并使用GET方法在iOS中使用了webservice,没有任何问题。但是,我一直无法使POST方法起作用

是否有任何方法可以检查服务器实现是否正确?在浏览器中输入URL不会调用POST方法。下面的方法可能有什么错误

这是我的服务器实现:

@Path("/v1/consumer/")
public class V1_consumer {


@POST
@Path("/updateConsumptionMetric")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response updateConsumptionMetric(String incomingData) throws Exception {
    String returnString = null;
    JSONArray jsonArray = new JSONArray();
    JSONObject jsonObject = new JSONObject();
    DBC dBC = new DBC();

    try{
        JSONObject partsData = new JSONObject(incomingData);
        System.out.println("jsonData: " + partsData.toString());

        int http_code = dBC.updateConsumptionMetricForConsumer(partsData.optInt("ConsumerID"), partsData.optInt("ConsumptionMetric"));
        if(http_code == 200){
            jsonObject.put("HTTP_CODE", "200");
            jsonObject.put("MSG", "Items has been entered successfully");
            returnString = jsonArray.put(jsonObject).toString();
        }else{
            return Response.status(500).entity("Unable to process items").build();
        } 
    } catch(Exception e){
        e.printStackTrace();
        return Response.status(500).entity("Server was not able to process your request").build();
    }
    return Response.ok(returnString).build();
}
-(void)updateConsumptionMetric:(NSNumber*)metricID forConsumer:(NSNumber*)consumerID{
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:consumerID, @"ConsumerID", metricID, @"ConsumptionMetric", nil];

NSData * JsonData =[NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONWritingPrettyPrinted error:nil];
NSString * jsonString= [[NSString alloc] initWithData:JsonData encoding:NSUTF8StringEncoding];

NSMutableData *body = [NSMutableData data];

[body appendData:[[NSString stringWithFormat:@"%@",jsonString] dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://profolo.ddns.net:8080/com.profolo.it.cdss.api/api/v1/consumer/updateConsumptionMetric"]]];
[request setHTTPBody:body];
[request setHTTPMethod:@"POST"];

[NSURLConnection sendAsynchronousRequest: request
                                   queue: queue
                       completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error) {
                           if (error || !data) {
                               // Handle the error

                               NSLog(@"Server Error : %@", error);
                           } else {
                               // Handle the success

                               NSLog(@"Server Response :%@",response);
                           }
                       }
 ];
这是我的iOS实现:

@Path("/v1/consumer/")
public class V1_consumer {


@POST
@Path("/updateConsumptionMetric")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response updateConsumptionMetric(String incomingData) throws Exception {
    String returnString = null;
    JSONArray jsonArray = new JSONArray();
    JSONObject jsonObject = new JSONObject();
    DBC dBC = new DBC();

    try{
        JSONObject partsData = new JSONObject(incomingData);
        System.out.println("jsonData: " + partsData.toString());

        int http_code = dBC.updateConsumptionMetricForConsumer(partsData.optInt("ConsumerID"), partsData.optInt("ConsumptionMetric"));
        if(http_code == 200){
            jsonObject.put("HTTP_CODE", "200");
            jsonObject.put("MSG", "Items has been entered successfully");
            returnString = jsonArray.put(jsonObject).toString();
        }else{
            return Response.status(500).entity("Unable to process items").build();
        } 
    } catch(Exception e){
        e.printStackTrace();
        return Response.status(500).entity("Server was not able to process your request").build();
    }
    return Response.ok(returnString).build();
}
-(void)updateConsumptionMetric:(NSNumber*)metricID forConsumer:(NSNumber*)consumerID{
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjectsAndKeys:consumerID, @"ConsumerID", metricID, @"ConsumptionMetric", nil];

NSData * JsonData =[NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONWritingPrettyPrinted error:nil];
NSString * jsonString= [[NSString alloc] initWithData:JsonData encoding:NSUTF8StringEncoding];

NSMutableData *body = [NSMutableData data];

[body appendData:[[NSString stringWithFormat:@"%@",jsonString] dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://profolo.ddns.net:8080/com.profolo.it.cdss.api/api/v1/consumer/updateConsumptionMetric"]]];
[request setHTTPBody:body];
[request setHTTPMethod:@"POST"];

[NSURLConnection sendAsynchronousRequest: request
                                   queue: queue
                       completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error) {
                           if (error || !data) {
                               // Handle the error

                               NSLog(@"Server Error : %@", error);
                           } else {
                               // Handle the success

                               NSLog(@"Server Response :%@",response);
                           }
                       }
 ];

}

我确信,输入浏览器的URL总是发出GET请求,而不是向Web服务发出POST请求。因此,我建议您应该使用另一个工具来进行POST请求(我建议使用)

还有一件事,您应该更改这行代码

public Response updateConsumptionMetric(String incomingData)


谢谢Tuan,通过使用Postman,我在Java代码中解决了这个问题,最后在iOS代码中也发现了一个错误。行[request setValue:@“application/json;charset=UTF-8”forHTTPHeaderField:@“Content Type”];他失踪了。