iphone - iPhone 中的 JSON Webservice 问题?

标签 iphone ios json web-services

目前我在 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 中集成相同的请求,但响应不同,如何解决这个问题? 请帮助我

提前致谢

最佳答案

你能检查一下吗

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

通过做这样的事情来检查字典中弹出的值

 NSDictionary *question = [jsonDict objectForKey:@"status"];

关于iphone - iPhone 中的 JSON Webservice 问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12789108/

相关文章:

iPhone - 当键盘已经显示时知道键盘尺寸

objective-c - x代码 : Passing a Label text into a custom added subview

objective-c - 如何解决CALayerInvalidGeometry',原因: 'CALayer position contains NaN: [nan nan]?

iphone - 限制 iPhone 中 UIButton 的可触摸区域?

python - 将 JSON 解码为 Python

javascript - 使用作为单引号字符串传递的值解析 JSON 对象

ios - AVAudioSession 播放时录音奇怪的问题

iphone - 使我的 iPhone 5 应用程序与 iPhone 4、iPod Touch、旧版 iPhone 兼容?

iphone - 使用赋值的结果作为不带括号的条件

javascript - npm start 没有获取更新的 package.json