Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何发布请求以获取jSON响应_Iphone_Objective C_Ios_Json - Fatal编程技术网

Iphone 如何发布请求以获取jSON响应

Iphone 如何发布请求以获取jSON响应,iphone,objective-c,ios,json,Iphone,Objective C,Ios,Json,我试图使用POST方法向键值对发送HTTP-POST请求,该方法反过来向我发送jSON响应 代码 //[dictionnary setObject:@"tId" forKey:@"serialnumber"]; NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary]; [dictionnary setObject:[NSString stringWithFormat:@"tId"] forKey:

我试图使用POST方法向键值对发送HTTP-POST请求,该方法反过来向我发送jSON响应

代码

//[dictionnary setObject:@"tId" forKey:@"serialnumber"];
 NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary];
         [dictionnary setObject:[NSString stringWithFormat:@"tId"] forKey:@"serialnumber"];
   [dictionnary setObject:[NSString stringWithFormat:@"tname"]  forKey:@"mobileimei"];
    [dictionnary setObject:[NSString stringWithFormat: @"tprice"] forKey:@"submerchantguid"];
    [dictionnary setObject:[NSString stringWithFormat: @"tquan"] forKey:@"transactionid"];
    [dictionnary setObject:[NSString stringWithFormat: @"tquan"] forKey:@"emailid"];
    [dictionnary setObject:[NSString stringWithFormat: @"tquan"] forKey:@"mobileno"];
    [dictionnary setObject:[NSString stringWithFormat: @"tquan"] forKey:@"signature"];
    [dictionnary setObject:[NSString stringWithFormat:@"tquan"] forKey:@"photo"];


 NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionnary
                                                       options:kNilOptions
                                                         error:&error];
    NSLog(@"Error is %@",error);

   NSString *urlString = @"MY_POST_URL/transaction/model/transactionsuccess";
    //NSString *urlString = @"http://yahoo.com";
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];

    [request setHTTPBody:jsonData];
    NSURLResponse *response = NULL;
    NSError *requestError = NULL;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
    NSLog(@"request Error %@",requestError);
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] ;
    NSLog(@"%@", responseString);
输出

inside submitinfo method
2012-11-29 12:13:02.238 ReaderDeployment[1568:11f03] Error is (null)
2012-11-29 12:13:03.005 ReaderDeployment[1568:11f03] request Error (null)
2012-11-29 12:13:03.006 ReaderDeployment[1568:11f03] <!DOCTYPE html PUBLIC
    "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>PHP notice</title>

<style type="text/css">
/*<![CDATA[*/
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,font,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td{border:0;outline:0;font-size:100%;vertical-align:baseline;background:transparent;margin:0;padding:0;}
body{line-height:1;}
ol,ul{list-style:none;}
blockquote,q{quotes:none;}
blockquote:before,blockquote:after,q:before,q:after{content:none;}
:focus{outline:0;}
ins{text-decoration:none;}
del{text-decoration:line-through;}
table{border-collapse:collapse;border-spacing:0;}

body {
    font: normal 9pt "Verdana";
    color: #000;
    background: #fff;
}

h1 {
    font: normal 18pt "Verdana";
    color: #f00;
    margin-bottom: .5em;
}

h2 {
    font: normal 14pt "Verdana";
    color: #800000;
    margin-bottom: .5em;
}

h3 {
    font: bold 11pt "Verdana";
}

pre {
    font: normal 11pt Menlo, Consolas, "Lucida Console", Monospace;
}

pre span.error {
    display: block;
    background: #fce3e3;
}

pre span.ln {
    color: #999;
    padding-right: 0.5em;
    border-right: 1px solid #ccc;
}

pre span.error-ln {
    font-weight: bold;
}

.container {
    margin: 1em 4em;
..........

格式也一样。我没有收到任何错误,仍然没有达到预期的jSON响应。后端只接收信息,并将状态抛出为“0”或“1”。相反,显示了一些奇怪的输出,我想是css脚本。

服务器端出现错误,您不能直接使用json,您必须先将json转换为数组json\u decode,然后使用数组的值

这是如何将php json转换为数组的

另外,要返回json,您应该将php数组再次格式化为json编码

[dictionnary setObject:@"tId" forKey:@"serialnumber"];