将数据从iPhone发布到服务器(解析数据)

时间:2014-02-18 07:10:52

标签: ios iphone

我在使用 jsonserialization

上传服务器上的数据时有点困惑

试图发送这样的数据,但它让我感到不舒服,我不知道怎么能用这个

我的格式就像这样

注册服务

网址:http://vps.XXXX.com/UserService.svc/XXXXX

RequestJson   :

{
    "RegDetails": {
        "Phone_Device_Id": "123",
        "Email_Id": “test@gmail.com",
        "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"
    }
}

我的方法

- (无效)sendReuest {

NSString *Post = [NSString stringWithFormat:@"name=XXXX&email=XXX@gmail.com&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];
}

}

上面的方法现在给我一个错误。有时候它给了我一个完整的HTML页面,有时它给了我什么。

请提供最好使用的建议。如何使用。这是我的问题。

3 个答案:

答案 0 :(得分:1)

请使用此代码检查此解决方法

-(void)sendJsonDate
{
    NSString*finalvalue=[NSString stringWithFormat:@"{\"RegDetails\":{\"Email_Id\”:\”XXXX@gmail.com\",\"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);
    }

}

答案 1 :(得分:0)

尝试更换

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

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

答案 2 :(得分:0)

好的,让我为你写一个代码:)

-(void)test
{
       NSDictionary *aDict=[NSDictionary dictionaryWithObjectsAndKeys:@"Harish",@"name",@"XXXX@gmail.com",@"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];
    }
}

我认为如果不向我发送网址和请求格式,它可能会有用。