0

私はiPhoneアプリを作成していて、パラメーターを含むWebサービスへのJSONリクエストを作成する方法を理解しようとしています。Javaでは、これは次のようになります

HashMap<String, String> headers = new HashMap<String, String>();

JSONObject json = new JSONObject();

            json.put("nid", null);
            json.put("vocab", null);
            json.put("inturl", testoverview);
            json.put("mail", username); // something@gmail.com
            json.put("md5pw", password); // donkeykong

        headers.put("X-FBR-App", json.toString());

サービスがリクエストを認識する前に、ヘッダーにJSONオブジェクトと「X-FBR-App」が含まれている必要があります。これはObjective-Cでどのように実装されますか?

4

2 に答える 2

1

Postメソッドを使用します。これを試してください

    NSString *method =  @"addProduct"; 
 NSString *jsonstring=@"http://yourdomain.com/product_review_api/product-review.php?"; 
 NSString *urlString = [NSString stringWithFormat:@"%@",jsonstring];
 NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
 [request setURL:[NSURL URLWithString:urlString]];
 [request setHTTPMethod:@"POST"]; 
  NSMutableData *body = [NSMutableData data];
    // image file
   NSData *imageData = UIImageJPEGRepresentation( labelimage, 0.8);
   NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

      //  parameter method
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"method\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[method dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

   //  parameter categoryid
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"category_id\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[selectedID dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];

    // now lets make the connection to the web
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(@"%@",returnString);
    NSDictionary *profile = (NSDictionary*)[returnString JSONValue];
    NSLog(@"profile=%@", profile); 

このようにすべてのパラメータを追加できます。

于 2012-04-10T11:40:09.403 に答える
1

JSONはiOSに組み込まれていませんが、www.JSON.orgを見ると、必要な機能を提供するObjective-Cフレームワークがいくつかあります。私はそれらのいずれも経験がないので、推奨することはできませんが、解決策を見つけるための良いスタートです。

このSOの質問post-data-through-json-webservices-in-iphoneもご覧ください

于 2012-04-10T12:01:06.423 に答える