dvtelles
dvtelles

Reputation: 200

RestKit Post method with JSON

I'm developing an iOS App for school. I'm using a database so i can run some statistics later. I created a Restful Web Service to handle all functions I need and using RestKit to access the Web Service. When I need to retrieve data from WS i dont have any problem to do it, but when i need to post info I am getting some errors that I would like some help if you can.

The POST method I created in WS is for adding a new Collection to DB, it has no return, just add it. I'm using the GSON library to convert from JSON. I tested with the "tester" from netbeans and worked well so I guess the problem is not on the Web Service. I'll put the code for the classes involved and the method that is trying to POST de object.

I have this class: Collection

#import <Foundation/Foundation.h>
#import "User.h"
@interface Collection : NSObject
@property NSNumber *idCollection;
@property NSString *name;
@property User *user;
@property NSArray *collectionItens;
@end

And this class: User

#import <Foundation/Foundation.h>
@interface User : NSObject
@property NSNumber *idUser;
@property NSString *login;
@property NSString *password;
@end

Both class only have the @syntezise

Here is the method im trying to post the object:

- (IBAction)createNewCollection:(id)sender
{
NSLog(@"..");
Collection *collection = [[Collection alloc] init];
collection.name = collectionNameTextField.text;
collection.user = [AppDefauts defaultUser];

RKObjectMapping *userMapping = [RKObjectMapping requestMapping];
[userMapping addAttributeMappingsFromArray:@[ @"idUser", @"login", @"password"]];

RKObjectMapping *collectionMapping = [RKObjectMapping requestMapping];
[collectionMapping addAttributeMappingsFromArray:@[ @"idCollection", @"name"]];

[collectionMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"user" toKeyPath:@"user" withMapping:userMapping]];



RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:collectionMapping objectClass:[Collection class] rootKeyPath:@"collection" method:RKRequestMethodAny];


RKObjectManager *objectManager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://localhost:8080/MTGAppWS/webresources"]];
[objectManager addRequestDescriptor:requestDescriptor];

[objectManager postObject:collection path:@"/Collection"    parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSLog(@"Success");

} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Error");
}];
}

Log display on Xcode with error:

http://pastebin.com/JXCYzF8B

Web Service code for include a new Collection

@Path("Collection")
public class CollectionService
{
}
@Context
private UriInfo context;

/**
 * Creates a new instance of CollectionService
 */
public CollectionService()
{
}
@PUT
@Consumes("application/json")
public void createNewCollection(String content)
{
    CollectionController c= new CollectionController(new SQLController());
    Gson gson = new Gson();
    JsonParser parser = new JsonParser();
    parser.parse(content);
    Collection collection = gson.fromJson(content, Collection.class);
    c.criarNovaCollection(collection);

}

}

EDIT

Found out that the request wasn't "going" to right address.

already fixed that but now its happening another error. (Method not allowed).

Error message: http://pastebin.com/99FiNmQZ

Upvotes: 0

Views: 320

Answers (1)

Josh The Geek
Josh The Geek

Reputation: 1213

The server you are communicating with is giving you an HTML error page instead of the JSON you were expecting: The requested resource () is not available. The error isn't in your app (unless you are asking for the wrong URL); it's on the server.

Upvotes: 1

Related Questions