2012-04-01 16 views
5

Webサービスメソッドを呼び出して、パラメータを渡そうとしています。ここでObjective CのJSON Webサービスへのパラメータの受け渡し

は、私のWebサービスメソッドである:ここで

[WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public void GetHelloWorld() 
    { 
     Context.Response.Write("HelloWorld"); 
     Context.Response.End(); 
    } 

    [WebMethod] 
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
    public void GetHelloWorldWithParam(string param) 
    { 
     Context.Response.Write("HelloWorld" + param); 
     Context.Response.End(); 
    } 

は私のObjective Cのコードです:

NSString *urlString = @"http://localhost:8080/MyWebservice.asmx/GetHelloWorld"; 
NSURL *url = [NSURL URLWithString:urlString]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
[request setHTTPMethod: @"POST"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 

NSError *errorReturned = nil; 
NSURLResponse *theResponse =[[NSURLResponse alloc]init]; 
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned]; 
if (errorReturned) 
{ 
    //...handle the error 
} 
else 
{ 
    NSString *retVal = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
    NSLog(@"%@", retVal); 
    //...do something with the returned value   
} 

私はGetHelloWorldを呼び出すときに、それは素晴らしい作品と:

NSLog(@"%@", retVal); 

表示のHelloWorld GetHelloWorldWithParamはどのように呼び出すのですか?パラメータを渡すには?

私は試してみてください。

NSMutableDictionary *dict = [NSMutableDictionary dictionary]; 
[dict setObject:@"myParameter" forKey:@"param"];  
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:&error]; 

と要求に2次の行を追加します。

[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"]; 
[request setHTTPBody: jsonData]; 

私がエラーを持っている:

System.InvalidOperationException: Missing parameter: test. 
    at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection) 
    at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters() 
    at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest() 

はあなたの助けをありがとう! テディ

答えて

2

で飾られていることを確認してください。あなたのコードを使用して少し修正しました。まず、次の試してみてください。

NSString *urlString = @"http://localhost:8080/MyWebservice.asmx/GetHelloWorldWithParam"; 
    NSURL *url = [NSURL URLWithString:urlString]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
    [request setHTTPMethod: @"POST"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
    NSString *myRequestString = @"param="; // Attention HERE!!!! 
    [myRequestString stringByAppendingString:myParamString]; 
    NSData *requestData = [NSData dataWithBytes:[myRequestString UTF8String] length:[myRequestString length]]; 
    [request setHTTPBody: requestData]; 

残りの部分は、(ラインNSError *errorReturned = nilから始まる)あなたのコードと同じです。

通常、このコードは正常に動作するはずです。しかし、あなたがweb.configに以下の変更を加えていない場合、それはされません。あなたのweb.configファイルに次の行が含まれている場合

チェック:私はこのようにそれを解決した

<configuration> 
    <system.web> 
    <webServices> 
     <protocols> 
      <add name="HttpGet"/> 
      <add name="HttpPost"/> 
     </protocols> 
    </webServices> 
    </system.web> 
</configuration> 

は、それはあまりにもあなたのために働く願っています。

あなたはより多くの情報が必要な場合は、これらの2つの以下の質問を参照してください。
. Add key/value pairs to NSMutableURLRequest
. Request format is unrecognized for URL unexpectedly ending in

+0

あまりにもありがとう、それは解決策です。私はparamを忘れました...私のweb.configはすでにこれらの行を持っていました。 – user1306602

1

レスポンスストリームの制御を手動で引き継ぎません。ほんの少し以下のようにあなたのWebサービスメソッドを変更します。

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public string GetHelloWorld() 
{ 
    return "HelloWorld"; 
} 

[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public string GetHelloWorldWithParam(string param) 
{ 
    return "HelloWorld" + param; 
} 

は、あなただけの見返りにJSONを提供したい場合は、[ScriptMethod(ResponseFormat = ResponseFormat.Json)]を追加していることを確認します。しかし、これを追加しないと、XMLとJsonの両方の要求を処理することができます。

P.S.あなたのWebサービスクラスが[ScriptService]

+0

[OK]を、私は以下のソリューションと私の問題を解決する、ありがとう。私はContext.Response.Writeの代わりにちょうどリターンを試してみるでしょう... – user1306602

関連する問題