0

現在、私はiPhoneアプリケーションで作業しており、JSONを使用してリクエストを送信し、それを読み取るためのレスポンスを取得しています。

リクエスト (URL の例):

http://www.Genifer.com/index.php?q=api/username-available&username=stephen

ブラウザー (Firefox) からの応答を参照します。

{"status":true,"result":true}

私はxcodeで試しました:

NSString *urlString = [NSString stringWithFormat:@" http://www.Genifer.com/index.php?"];
        NSString *parameter = [NSString stringWithFormat:@"q=api/username-available&username=stephen"];   

        NSData *parameterData = [parameter dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
        NSURL *url = [NSURL URLWithString:urlString];

        NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

        [theRequest addValue: @"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        [theRequest setHTTPMethod:@"POST"];
        [theRequest setHTTPBody:parameterData]; 

        NSURLConnection *connection = [[NSURLConnection alloc] 
                                       initWithRequest:theRequest delegate:self];

        if( connection )
        {
            mutableData = [[NSMutableData alloc] init];
        }
        else 
        {

        } 


-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response
{
    [mutableData setLength:0]; 
    NSLog(@"mutableData:%@",mutableData);
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [mutableData appendData:data];
    NSLog(@"mutableData:%@",mutableData);

}

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [mutableData release];
    [connection release];
    return;
}


-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{


        NSString *jsonStr = [[NSString alloc] initWithData:mutableData encoding:NSUTF8StringEncoding]; 
        NSLog(@"JSonSTr : %@", jsonStr);

}

応答が来ます:

 JSonSTr : <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Firefox ブラウザでリクエストを参照すると、{"status":true,"result":true} のようなレスポンスが返されます。しかし、同じリクエストをxcodeに統合しようとしましたが、レスポンスが異なります。これを修正するにはどうすればよいですか? 私を助けてください

前もって感謝します

4

1 に答える 1

2

これで確認できますか

NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [jsonString JSONValue];

次のようにして、辞書に表示される値を確認します

 NSDictionary *question = [jsonDict objectForKey:@"status"];
于 2012-10-08T20:48:03.863 に答える