【问题标题】:How can i send my data to server through URL.[iOS] [duplicate]我如何通过 URL 将我的数据发送到服务器。[iOS] [重复]
【发布时间】:2014-07-27 07:47:30
【问题描述】:

如何使用 POST 方法通过 URL 向服务器发送数据。

我的数据如下:

  json == {
Signup =     {
    email = test;
    password = 123;
    username = test;
 };
}

我的网址是这样的:

http://192.168.1.122/~test/sample/index.php/Api/signup

请给我建议。从过去的两天开始,我一直坚持这一点。请帮帮我。

数据格式为 JSON。

【问题讨论】:

  • 你需要什么结果,你需要将json数据发送到192.168.1.122/~test/sample/index.php/Api/signup
  • 是的,我想将 JSON 数据发送到这个 url。请帮帮我
  • 如果你没有我的,你可以给 json == { Signup = { email = test;密码 = 123;用户名 = 测试; }; } ----> 这个网址

标签: ios json


【解决方案1】:

您可以执行类似的操作,使用 JSON 数据发送简单的发布请求

  -(void)sendPostData{

 NSString *urlStr = @"http://me.com";
 urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 NSURL *url = [NSURL URLWithString:urlStr];

NSDictionary* info = [NSDictionary dictionaryWithObjectsAndKeys:user.userName,@"username",user.password,@"password",user.email,@"email", nil];
    NSError *error;

   NSData* bodyData = [NSJSONSerialization dataWithJSONObject:info
                                                       options:kNilOptions error:&error];


    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:data];
    [request setValue:[NSString stringWithFormat:@"%d", [data length]] forHTTPHeaderField:@"Content-Length"];


    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *responseFromRequest, NSData *data, NSError *error)
     {
         NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)responseFromRequest;
         NSInteger code = [httpResponse statusCode];

         }];
}

【讨论】:

  • 什么是user.userName?我的 q 是什么用户?
  • 把你的用户名放在那里。您尝试发送的用户名。与其他参数相同。
【解决方案2】:

看看我的 GitHub 存储库 JWURLConnectionenter link description here,这会对你有所帮助。

如果您以 API 为目标,我也会推荐 JWRESTClient。

【讨论】:

    猜你喜欢
    • 2018-03-27
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    • 2013-11-04
    • 1970-01-01
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多