2012-02-02 99 views
0

任何人都可以幫我嗎?我長期困擾這個問題。iOS JSON與ASPNET Web服務

我正在開發一個IOS來將JSON引入Web服務以便插入到數據庫中。 但未能做到。

IOS源代碼

-(IBAction)signup{ 

    NSMutableDictionary * dic = [NSMutableDictionary dictionary]; 
    [dic setObject:@"0" forKey:@"account_id"]; 
    [dic setObject:@"Jin" forKey:@"first_name"]; 
    [dic setObject:@"Lim" forKey:@"last_name"]; 
    [dic setObject:@"1987-12-02" forKey:@"date_of_birth"]; 
    [dic setObject:@"M" forKey:@"account_gender"]; 
    [dic setObject:@"65" forKey:@"phone_country_id"]; 
    [dic setObject:@"123456" forKey:@"account_phone"]; 
    [dic setObject:@"[email protected]" forKey:@"account_login"]; 

    [svc execMethod:dic]; 
} 

然後

-(void) execMethod:(NSMutableDictionary *)dic{ 

    NSURL * url = [NSURL URLWithString:@"http://test.biz/AccountInfo.asmx/insert_account"]; 

    //Request 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; 
    NSData* requestData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil]; 

    //prepare http body 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:[NSString stringWithFormat:@"%d",[requestData length]] forHTTPHeaderField:@"Content-Length"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    [request setHTTPBody:requestData]; 

    urlConnection = [[NSURLConnection alloc]initWithRequest:request delegate:self startImmediately:YES]; 

    UIApplication *app = [UIApplication sharedApplication]; 
    app.networkActivityIndicatorVisible = YES; 
} 

Web服務

{WebMethod()} _ 
{ScriptMethod(ResponseFormat:=ResponseFormat.Json, UseHttpGet:=False)}_ 
Public Function insert_account(ByVal result As Account) As String 

    Dim account As New Account 
    account = result 
    Dim msg As String 

    **Processing Result*** 

    Return "msg:'OK'" 

End Function 

P/S:我用{}來代替> <爲web服務方法

錯誤的NSLog

2012-02-02 16:25:11.433 test.biz[10642:f803] System.InvalidOperationException: insert_account Web Service method name is not valid. 
    at System.Web.Services.Protocols.HttpServerProtocol.Initialize() 
    at System.Web.Services.Protocols.ServerProtocol.SetContext(Type type, HttpContext context, HttpRequest request, HttpResponse response) 
    at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing) 

煩請告訴我,我有什麼錯完成,感謝您的幫助:)

+0

你能從瀏覽器訪問網頁方法嗎? – AnthonyBlake 2012-02-02 11:51:47

+0

也是,你使用的是什麼版本的.net? – AnthonyBlake 2012-02-02 11:55:24

+0

你測試過你的網絡服務嗎?我的猜測是,您沒有正確配置Web服務,無論是在web.config文件中還是使用註釋(例如'')。我暫時把XCode項目放在一邊,啓動Fiddler,在那裏建立請求並用它測試Web服務。 – Codo 2012-02-02 12:27:40

回答

0

我找到了解決辦法:

-(IBAction)signup{ 

    NSMutableDictionary * dic = [NSMutableDictionary dictionary]; 
    [dic setObject:@"0" forKey:@"account_id"]; 
    [dic setObject:@"Jin" forKey:@"first_name"]; 
    [dic setObject:@"Lim" forKey:@"last_name"]; 
    [dic setObject:@"1987-12-02" forKey:@"date_of_birth"]; 
    [dic setObject:@"M" forKey:@"account_gender"]; 
    [dic setObject:@"65" forKey:@"phone_country_id"]; 
    [dic setObject:@"123456" forKey:@"account_phone"]; 
    [dic setObject:@"[email protected]" forKey:@"account_login"]; 

    NSMutableDictionary * result = [NSMutableDictionary dictionary]; 
    [result setObject:dic forKey:@"result"]; 

    NSURL * url = [NSURL URLWithString:@"http://test.biz/AccountInfo.asmx/insert_account"]; 

    svc = [[JSONRPCService alloc] initWithURL:url]; 

    [svc execMethod:result]; 
} 

只需要爲JSON添加標頭。