简体   繁体   中英

Reading jSON object request via key-value pair

I am trying to read jSON response in form of key value pair.

When I am manually hitting the URL via browser I am getting the required status code/response via jSON object

{"status":"0","responseCode":"1021","response":{"message":"Please check your Settings ","data":"2012-11-24, 7:28 am"}}

The same expected response is not obtained when I am trying to do it via my app.My code when I am sending data via app

[dictionnary setObject:@"admin"  forKey:@"username"];
[dictionnary setObject:@"123123" forKey:@"password"];

NSError *error = nil;

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionnary
                                                   options:kNilOptions
                                                     error:&error];

NSString *urlString = @"MY CALL URL";

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(@"response is obtained");

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] ;
NSLog(@"%@", responseString);

Response I am getting via code

<!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;
    }

    .version {
        color: gray;
        font-size: 8pt;
        border-top: 1px solid #aaa;
        padding-top: 1em;
        margin-bottom: 1em;
    }

    .message {
        color: #000;
        padding: 1em;
        font-size: 11pt;
        background: #f3f3f3;
        -webkit-border-radius: 10px;
        -moz-border-radius: 10px;
        border-radius: 10px;
        margin-bottom: 1em;
        line-height: 160%;
    }

    .source {
        margin-bottom: 1em;
    }

    .code pre {
        background-color: #ffe;
        margin: 0.5em 0;
        padding: 0.5em;
        line-height: 125%;
        border: 1px solid #eee;
    }

    .source .file {
        margin-bottom: 1em;
        font-weight: bold;
    }

    .traces {
        margin: 2em 0;
    }

    .trace {
        margin: 0.5em 0;
        padding: 0.5em;
    }

    .trace.app {
        border: 1px dashed #c00;
    }

    .trace .number {
        text-align: right;
        width: 2em;
        padding: 0.5em;
    }

    .trace .content {
        padding: 0.5em;
    }

    .trace .plus,
    .trace .minus {
        display:inline;
        vertical-align:middle;
        text-align:center;
        border:1px solid #000;
        color:#000;
        font-size:10px;
        line-height:10px;
        margin:0;
        padding:0 1px;
        width:10px;
        height:10px;
    }

    .trace.collapsed .minus,
    .trace.expanded .plus,
    .trace.collapsed pre {
        display: none;
    }

    .trace-file {
        cursor: pointer;
        padding: 0.2em;
    }

    .trace-file:hover {
        background: #f0ffff;
    }
    /*]]>*/
    </style>
    </head>

    <body>
    <div class="container">
        <h1>PHP notice</h1>

        <p class="message">
            Undefined index: signature  </p>

        <div class="source">
            <p class="file">/var/www/protected/controllers/Authenticate.php(955)</p>
            <div class="code"><pre><span class="ln">943</span>   

            <td class="number">
                #7          </td>
            <td class="content">
                <div class="trace-file">
                                            <div class="plus">+</div>
                        <div class="minus">–</div>
                                        &nbsp;/var/yii-1.1.10.r3566/framework/base/CApplication.php(162): <strong>CWebApplication</strong>-><strong>processRequest</strong>()               </div>

                <div class="code"><pre><span class="ln">157</span>      */
<span class="ln">158</span>     public function run()
<span class="ln">159</span>     {
<span class="ln">160</span>         if($this-&gt;hasEventHandler(&#039;onBeginRequest&#039;))
<span class="ln">161</span>             $this-&gt;onBeginRequest(new CEvent($this));
<span class="error"><span class="ln error-ln">162</span>         $this-&gt;processRequest();
</span><span class="ln">163</span>         if($this-&gt;hasEventHandler(&#039;onEndRequest&#039;))
<span class="ln">164</span>             $this-&gt;onEndRequest(new CEvent($this));
<span class="ln">165</span>     }
<span class="ln">166</span> 
<span class="ln">167</span>     /**
</pre></div>            </td>
        </tr>
                        <tr class="trace app expanded">
            <td class="number">
                #8          </td>
            <td class="content">
                <div class="trace-file">
                                            <div class="plus">+</div>
                        <div class="minus">–</div>
                                        &nbsp;/var/www/index.php(13): <strong>CApplication</strong>-><strong>run</strong>()             </div>

                <div class="code"><pre><span class="ln">08</span> defined(&#039;YII_DEBUG&#039;) or define(&#039;YII_DEBUG&#039;,true);
<span class="ln">09</span> // specify how many levels of call stack should be shown in each log message
<span class="ln">10</span> defined(&#039;YII_TRACE_LEVEL&#039;) or define(&#039;YII_TRACE_LEVEL&#039;,3);
<span class="ln">11</span> 
<span class="ln">12</span> require_once($yii);
<span class="error"><span class="ln error-ln">13</span> Yii::createWebApplication($config)-&gt;run();
</span></pre></div>         </td>
        </tr>
                </table>
    </div>

    <div class="version">
        2012-11-24 07:38:18 Apache/2.2.22 (Ubuntu) <a href="http://www.yiiframework.com/">Yii Framework</a>/1.1.10  </div>
</div>

<script type="text/javascript">
/*<![CDATA[*/
var traceReg = new RegExp("(^|\\s)trace-file(\\s|$)");
var collapsedReg = new RegExp("(^|\\s)collapsed(\\s|$)");

var e = document.getElementsByTagName("div");
for(var j=0,len=e.length;j<len;j++){
    if(traceReg.test(e[j].className)){
        e[j].onclick = function(){
            var trace = this.parentNode.parentNode;
            if(collapsedReg.test(trace.className))
                trace.className = trace.className.replace("collapsed", "expanded");
            else
                trace.className = trace.className.replace("expanded", "collapsed");
        }
    }
}
/*]]>*/
</script>

</body>
</html>

specify in your URLRequest that you dont want html but you want raw json

NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];

to:

NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];//!!!

You can try the following code May be help you.

NSString *username = emailField.text;
        NSString *password = passwordField.text;
        NSMutableDictionary *dictionnary = [NSMutableDictionary dictionary];
        [dictionnary setObject:@"admin"  forKey:@"username"];
        [dictionnary setObject:@"123456" forKey:@"password"];

        NSError *error = nil;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionnary
                                                           options:kNilOptions
                                                             error:&error];   

        NSString *urlString = @"Sample URL";

        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];
        NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding] ;
         NSLog(@"%@", responseString); 

if it is a GET Request then, can you try link : /index.php/api/transaction/model/transactionsuccess?username=admin&password=1212‌​3

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"/index.php/api/transaction/model/transactionsuccess?username=adm‌​in&password=1212‌​3"]];

 // Perform request and get JSON as a NSData object


NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
 NSLog(@"response=%@",response ); 

and use this code.

This problem is related to server side; so you must consult to back-end developer of the service and more possibly, check at service side the data hit using POST or GET, and if back-end is of php, so for a moment advised him to use $request instead of $get or $post, just to test. Also, this fact is obvious as you mentioned that as you tried the url from browser you get the required json, but in the case of POST you never able to do that.

I'm 100% sure this is from server side.If possible provide me the required server url with test auth variables. (cross-check your full url with the params as you provide either via break points or via log.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM