Harish
Harish

Reputation: 609

posting the data from iPhone to server (parsing data )

i am little bit confused in in uploading data on the server with jsonserialization

trying to send data like this but it make me uncomfortable,i don't how can i use this

my formate is like this

Registraion Service

URL : http://vps.XXXX.com/UserService.svc/XXXXX

RequestJson   :

{
    "RegDetails": {
        "Phone_Device_Id": "123",
        "Email_Id": “[email protected]",
        "First_Name": “testname”,
        "Last_Name": “testlastname,
        "DOB": "2014-02-11 00:00:00.000",
        "MemberShip_Type": "Annual"
    }
}


ResponseJson :   

{
    "Head": {
        "ResponseCode": "1",
        "ResponseText": "Success"
    },
 "UserDetails": {
        "UserId": "1"
    }
}

MY METHODS

-(void)sendReuest {

NSString *Post = [NSString stringWithFormat:@"name=XXXX&[email protected]&mobile=7894521456"];
NSData *Postdata = [Post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *PostLength = [NSString stringWithFormat:@"%d",[Postdata length]];


NSMutableURLRequest  *request = [[NSMutableURLRequest alloc]init];

[request setURL:[NSURL URLWithString:@"http://vps.XXXXX.com/TestService.svc/XXXXX?"] ];

[request setHTTPMethod:@"POST"];

[request setValue:PostLength forHTTPHeaderField:@"Content-Length"];


[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:Postdata];
NSLog(@"%@",request);
NSURLConnection *theconnection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(theconnection)
{
    webData = [[NSMutableData alloc]init];
}

}

The method above gives me an error now. Some times it gives me an entire html page and some times it gives me nothing.

Please give suggestion which the best to use .How to use .That's my question.

Upvotes: 0

Views: 121

Answers (3)

Harish
Harish

Reputation: 609

please check this solve with help this code

-(void)sendJsonDate
{
    NSString*finalvalue=[NSString stringWithFormat:@"{\"RegDetails\":{\"Email_Id\”:\”[email protected]\",\"DOB\":\"04/02/1986\",\"First_Name\”:\”XXXX\”,\”Last_Name\”:\”XXX\”,\”Phone_Device_Id\":\"896\",\"MemberShip_Type\":\"Annual\"}}"];
    NSLog(@"Request: %@", finalvalue);
    NSData *myJSONData =[finalvalue dataUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"myJSONString :%@", finalvalue);
    NSLog(@"myJSONData :%@", myJSONData);
    NSString *postLength = [NSString stringWithFormat:@"%d", [myJSONData length]];
    NSLog(@"%@",postLength);
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://vps.XXXXXXX.com/XXXXXXX.svc/XXXXXX”]]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:myJSONData];
    NSLog(@"%@",request);

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    NSLog(@"%@",theConnection);
    if (theConnection) {
        webData = [NSMutableData data];
        NSLog(@"%@",webData);
    }

}

Upvotes: 1

Rahul Mathur
Rahul Mathur

Reputation: 882

Ok let me write a code for you :)

-(void)test
{
       NSDictionary *aDict=[NSDictionary dictionaryWithObjectsAndKeys:@"Harish",@"name",@"[email protected]",@"email",@"123445",@"mobile", nil];


    NSData * Postdata = [NSJSONSerialization dataWithJSONObject:aDict options:NSJSONWritingPrettyPrinted error:Nil ];
     NSString *PostLength = [NSString stringWithFormat:@"%lu",(unsigned long)[Postdata length]];
    NSMutableURLRequest  *request = [[NSMutableURLRequest alloc]init];

    [request setURL:[NSURL URLWithString:@"http://vps.XXXX.com/TestService.svc/XXX1?"] ];

    [request setHTTPMethod:@"POST"];

    [request setValue:PostLength forHTTPHeaderField:@"Content-Length"];


    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:Postdata];
    NSLog(@"%@",request);
    NSURLConnection *theconnection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
    if(theconnection)
    {
        webData = [[NSMutableData alloc]init];
    }
}

I think it might work, if not send me the url and request format.

Upvotes: 0

Rahul Mathur
Rahul Mathur

Reputation: 882

Try to replace your

NSData *Postdata = [Post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
with

NSData * jsonData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&error ];

Upvotes: 0

Related Questions