2012-04-10 19 views
0

iPhoneアプリケーションを作成していて、パラメータを含むWebサービスへのJSONリクエストを作成する方法を見つけようとしています。 Javaではこれは次のようになりますObjective-Cでパラメータを使用してJSONリクエストを送信する方法

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); // [email protected] 
      json.put("md5pw", password); // donkeykong 

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

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

答えて

1

使用後は、あなたがこのようなすべてのパラメータを追加することができ、この

NSString *method = @"addProduct"; 
NSString *[email protected]"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); 

をmethod.try。

+0

ありがとうございました。私はこれまでに1つの質問があります。あなたが送ったコードでは、私のContent-Typeは - > "X-FBR-App"でしょうか? – Lahib

+0

コンテンツタイプを「X-FBR-App」 –

+0

に置き換えてみてください。このリンクをご利用いただきありがとうございます。このhttp://stackoverflow.com/questions/4085978/json-post-request-on-the-iphone-using-https –

1

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

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

+0

iOS 5.0とOS X 10.7からJSONはCocoaに焼き付けられています。http://developer.apple.com/library/ios/#documentation/Foundation/Reference/NSJSONSerialization_Class/Reference/Reference.html – JeremyP

+0

@JeremyPよく見せてくれます愚かな..ありがとう! –

関連する問題