2015-10-11 37 views
0

我消耗內搭參數這樣發送HTTP身體作爲使用AFNetworking或NSURLConnection的

jsonObj = { 
    "id" : 0, 
    "platform" : 1, 
    "method" : "Home" 
} 

一個POST網絡服務因此,如何把它的參數我使用AFNetworking消費web服務的字符串。

我的代碼

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
manager.responseSerializer = [AFHTTPResponseSerializer serializer]; 
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; 
[manager.requestSerializer setValue:ContentType forHTTPHeaderField:@"Content-Type"]; 


NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:0],@"id",[NSNumber numberWithInt:1],@"platform",API_HomeProductList,@"method", nil]; 
NSDictionary *param =[NSDictionary dictionaryWithObjectsAndKeys:dic,@"jsonObj", nil]; 
[manager POST:APIMainURL parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) { 

    NSMutableDictionary *json = [[NSMutableDictionary alloc]initWithDictionary:[NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil]]; // 
    NSLog(@"JSON: %@",json); 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 


} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 

}]; 

當我看到AFNetworking將其轉換成JSON後,我的參數,它成爲有效的JSON

{ 
    "jsonObj" = { 
    "id" : 0, 
    "platform" : 1, 
    "method" : "Home" 
    } 
} 

,但我得到一些錯誤,導致我的網絡服務採取

jsonObj = { 
    "id" : 0, 
    "platform" : 1, 
    "method" : "Home" 
} 

作爲參數

所以,請建議我如何在參數發送

jsonObj = { 
    "id" : 0, 
    "platform" : 1, 
    "method" : "Home" 
} 

+0

看到此鏈接可能是幫助你的http:// stackoverflow.com/questions/21344580/afnetworking-posts-json-arrays-as-multiple-single-entry-dictionaries –

回答

0

我想你誤解了一些東西。你的網絡需要

jsonObj={ 
    "id" : 0, 
    "platform" : 1, 
    "method" : "Home" 
} 

這意味着你需要創建一個JSON

{ 
    "id" : 0, 
    "platform" : 1, 
    "method" : "Home" 
} 

所以,你只需要刪除1行代碼

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
manager.responseSerializer = [AFHTTPResponseSerializer serializer]; 
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"]; 
[manager.requestSerializer setValue:ContentType forHTTPHeaderField:@"Content-Type"]; 


NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:0],@"id",[NSNumber numberWithInt:1],@"platform",API_HomeProductList,@"method", nil]; 
// NSDictionary *param =[NSDictionary dictionaryWithObjectsAndKeys:dic,@"jsonObj", nil]; /// remove this 
[manager POST:APIMainURL parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) { 

    NSMutableDictionary *json = [[NSMutableDictionary alloc]initWithDictionary:[NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil]]; // 
    NSLog(@"JSON: %@",json); 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 


} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 

}]; 
+0

no webservice takes jsonObj = { 「id」:0, 「platform」:1, 「方法」:「首頁」 }如果我按指示顯示錯誤。我必須通過懸而未決的jsonObj =來通過內部的json。 – user100

相關問題