2013-08-31 24 views
2

Web服務基於Python的ladon框架。如何從ios應用程序調用python的ladon框架開發的web服務?

Web服務URL是:http://ladonize.org/python-demos/Calculator

而且 代碼調用Web服務如下:

NSString *requestString = [NSString stringWithFormat: 
           @"http://ladonize.org/python-demos/Calculator"]; 
    NSString *jsonString = [NSString stringWithFormat:@"{\"methodname\":\"add\",\"args\": {\"a\":\"123\",\"b\":\"123\"}}"]; 
    NSData *jsonData = [NSData dataWithBytes: [jsonString UTF8String] length: [jsonString length]]; 
    NSURL *url = [NSURL URLWithString:requestString]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
    [request setHTTPMethod:@"POST"]; 
    [request setHTTPBody: jsonData]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
    [request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"]; 

    NSError *errorReturned = nil; 
    NSURLResponse *theResponse =[[NSURLResponse alloc]init]; 
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned]; 

    if (errorReturned) { 
     NSLog(@"Error %@",errorReturned.description); 
    } 
    else 
    { 
     NSError *jsonParsingError = nil; 
     NSMutableArray *arrDoctorInfo = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&jsonParsingError]; 
     NSString* newStr = [NSString stringWithUTF8String:[data bytes]]; 
     NSLog(@"Dict %@",newStr); 
    } 

而且我得到如下回應

<html> 
    <head> 
     <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" /> 
     <title>Calculator</title> 
     <style> 

    body { margin: 0px; padding: 0px; font-family: verdana; font-size: 12pt; background-color: #FCFCFC; } 

    div.service-header { 
     height: 34px; 
     font-weight: bold; padding: 10px; font-size: 30px; color: white; border-bottom: 1px solid black; background: #70803C; 
     filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#A0B06C', endColorstr='#70803C'); background: -webkit-gradient(linear, left top, left bottom, from(#A0B06C), to(#70803C)); background: -moz-linear-gradient(top, #A0B06C, #70803C); 
     text-shadow: #555555 2px 3px 3px; 
    } 
    div.service-header div.service-title {position: relative; float:left} 
    div.service-header .skin-selector {padding-top: 8px; margin-bottom: 0; position:relative; float: right; font-size: 12pt;} 

    div.service-overview { 
     float: right; margin: 20px; width: 200px; 
     -moz-border-radius: 7px 7px 7px 7px; -webkit-border-radius: 7px 7px 7px 7px; border-radius: 7px 7px 7px 7px; background-color: #F6F6F6; border: 1px solid #E6E6E6; border-collapse: separate; font-size: 11px; padding: 10px; margin-right: 20px; 
     -moz-box-shadow: 2px 2px 4px #555; -webkit-box-shadow: 2px 2px 4px #555; box-shadow: 2px 2px 4px #555; 
    } 

    div.service-overview div.headline { font-weight: bold; font-size: 18px; } 
    div.service-overview div.title { font-weight: bold; margin: 4px; font-size: 1.2em; } 
    div.service-overview ul.list { padding: 0px; margin: 0px; margin-left: 15px; list-style-type: none; } 
    div.service-overview ul.list li { font-size: 1.1em; } 

    div.service-description { padding: 15px; padding-bottom: 0px; } 
    div.service-description div.title { font-weight: bold; font-size: 1.2em; } 
    div.service-description p.doc-lines { font-size: 0.8em; } 
    div.service-description p.url { font-size: 0.8em; font-style: italic; } 
    div.service-description p.url span.url-title { font-weight: bold; } 

    div.service-interfaces { padding: 15px; padding-bottom: 0px; } 
    div.service-interfaces div.title { font-weight: bold; font-size: 1.2em; } 
    div.service-interfaces ul.list { font-size: 0.8em; } 
    div.service-interfaces ul.list li { padding: 4px; } 

    div.service-api { padding: 15px; padding-bottom: 0px; } 
    div.service-api div.methods div.title { font-weight: bold; font-size: 1.2em; } 
    div.service-api div.methods ul.list { font-size: 0.9em; list-style-type: none; } 
    div.service-api div.methods ul.list li.entry { 
     -moz-border-radius: 7px 7px 7px 7px; -webkit-border-radius: 7px 7px 7px 7px; border-radius: 7px 7px 7px 7px; background-color: #F6F6F6; border: 1px solid #E6E6E6; 
     border-collapse: separate; font-size: 0.8em; padding: 10px; margin-right: 20px; 
     -moz-box-shadow: 2px 2px 5px #555; -webkit-box-shadow: 2px 2px 5px #555; box-shadow: 2px 2px 5px #555; 
     margin-bottom: 16px; 
    } 
    div.service-api div.methods ul.list li.entry div.declaration { 
     font-size: 1.5em; 
    } 
    div.service-api div.methods ul.list li.entry div.declaration span.name { color: #881a1a; } 
    div.service-api div.methods ul.list li.entry span.param-type { color: #68387f; } 
    div.service-api div.methods ul.list li.entry span.param-name {} 
    div.service-api div.methods ul.list li.entry p.doc-lines { font-size: 1.2em; color: #276d11; } 
    div.service-api div.methods ul.list li.entry ul.params p.doc-lines { margin-top:0;margin-bottom:6px; font-size: 1.0em; color: #276d11; } 

    div.service-api div.types div.title { font-weight: bold; font-size: 1.2em; } 
    div.service-api div.types ul.list { font-size: 0.9em; list-style-type: none; } 
    div.service-api div.types ul.list li.entry { 
     -moz-border-radius: 7px 7px 7px 7px; -webkit-border-radius: 7px 7px 7px 7px; border-radius: 7px 7px 7px 7px; background-color: #F6F6F6; border: 1px solid #E6E6E6; 
     border-collapse: separate; font-size: 0.8em; padding: 10px; margin-right: 20px; 
     -moz-box-shadow: 2px 2px 5px #555; -webkit-box-shadow: 2px 2px 5px #555; box-shadow: 2px 2px 5px #555; 
     margin-bottom: 16px; 
    } 
    div.service-api div.types ul.list li.entry div.definition { 
     font-size: 1.5em; 
    } 
    div.service-api div.types ul.list li.entry div.definition span.name { color: #881a1a; } 
    div.service-api div.types ul.list li.entry span.param-type { color: #68387f; } 
    div.service-api div.types ul.list li.entry span.param-name {} 
    div.service-api div.types ul.list li.entry p.doc-lines { margin-top:0;margin-bottom:6px; font-size: 1.0em; color: #276d11; } 

    div.service-footer { font-size: 0.8em; text-align: center; font-style: italic; padding-top: 10px; padding-bottom: 10px; } 

    a:link { color: #00732F; text-decoration: none } 
    a:visited { color: #00732F; text-decoration: none } 
    a:hover { color: #00732F; text-decoration: underline } 

     </style> 
    </head> 
    <body> 
     <div class="service-header"> 
      <div class="service-title">Calculator</div> 
      <form method="get" class="skin-selector"> 
       <label for="skin-select">skins:</label> 
       <select id="skin-select" name="skin" onchange="document.forms[0].submit()"> 
        <option value="">Default</option> 

        <option value="simple">Simple</option> 

        <option value="bluebox">Bluebox</option> 

       </select> 
      </form> 
     </div> 
     <div class="service-overview"> 
      <div class="headline">Overview</div> 
      <div class="title">Methods</div> 
      <ul class="list"> 

       <li> 
        <a href="#add"><span class="entry">add</span></a> 
        () 
       </li> 

      </ul> 
      <div class="title">Types</div> 
      <ul class="list"> 

      </ul> 
     </div> 

     <div class="service-description"> 
      <div class="title">Description</div> 
      <p class="doc-lines"> 
       This service does the math, and serves as example for new potential Ladon users. 
      </p> 
     </div> 
     <div class="service-interfaces"> 
      <div class="title">Interfaces</div> 
      <ul class="list"> 

       <li>soap11 [ <a href="http://ladonize.org/python-demos/Calculator/soap11">url</a> <a href="http://ladonize.org/python-demos/Calculator/soap11/description">description</a> ]</li> 

       <li>jsonwsp [ <a href="http://ladonize.org/python-demos/Calculator/jsonwsp">url</a> <a href="http://ladonize.org/python-demos/Calculator/jsonwsp/description">description</a> ]</li> 

       <li>soap [ <a href="http://ladonize.org/python-demos/Calculator/soap">url</a> <a href="http://ladonize.org/python-demos/Calculator/soap/description">description</a> ]</li> 

      </ul> 
     </div> 
     <div class="service-api"> 
      <div class="methods"> 
       <div class="title">Methods</div> 
       <ul class="list"> 

        <li class="entry"> 
         <div class="declaration"> 
          <a name="add"></a><span class="name">add</span> 
          (



          <span class="param-type"> 

           number 

          </span> 
          <span class="param-name">a</span> 


          , 
          <span class="param-type"> 

           number 

          </span> 
          <span class="param-name">b</span> 


          ) 
         </div> 
         <p class="doc-lines"> 
          Add two integers together and return the result 

         </p> 


         <div class="params-title">Parameters</div> 
         <ul class="params"> 

          <li> 
           <span class="param-name">a</span>: <span class="param-type"> 

            number 

           </span> 

           <br/> 
           <p class="doc-lines"> 
            1st integer 
           </p> 
          </li> 



          <li> 
           <span class="param-name">b</span>: <span class="param-type"> 

            number 

           </span> 

           <br/> 
           <p class="doc-lines"> 
            2nd integer 
           </p> 
          </li> 

         </ul> 


         <div class="return-type-title">Return value</div> 
         <div class="return-type"> 
          <span class="param-type"> 

           number 

          </span> 
          <p class="doc-lines"> 
           The result of the addition 
          </p> 
         </div> 
        </li> 

       </ul> 
      </div> 
      <div class="types"> 
       <div class="title">Types</div> 
       <ul class="list"> 

       </ul> 
      </div> 
     </div> 
     <div class="service-footer">Powered by Ladon for Python</div> 
    </body> 
</html> 

請幫我解決這個問題。

在此先感謝。

+0

@Wain這麼多的信息是inof? – Learner

+0

是的,更好。顯示您已嘗試過某些方法並更好地描述問題。 – Wain

+0

看看用'dataUsingEncoding:'創建你的'jsonData'。 – Wain

回答

2

它看起來像你使用的URL路徑是錯誤的,應改爲http://ladonize.org/python-demos/Calculator/jsonwsp

創建jsonData的更好方法是使用dataUsingEncoding:

你不應該創建響應:

NSURLResponse *theResponse =[[NSURLResponse alloc]init]; 

因爲這將是創建並返回到您的sendSynchronousRequest的一部分。你應該做的是檢查theResponse的內容來驗證HTTP狀態碼。

+0

謝謝你現在工作.. :) – Learner

2

這是工作的解決方案...

NSString *requestString = [NSString stringWithFormat: 
           @"http://ladonize.org/python-demos/Calculator/jsonwsp"]; 
    NSString *jsonString = [NSString stringWithFormat:@"{\"methodname\":\"add\",\"args\": {\"a\":123,\"b\":123}}"]; 
    //: {"methodname": "add", "args": {"a":"123","b":"123"}} 
    NSLog(@"the request string is %@", requestString); 
    NSData *jsonData = [NSData dataWithBytes: [jsonString UTF8String] length: [jsonString length]]; 


NSURL *url = [NSURL URLWithString:requestString]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
[request setHTTPMethod:@"POST"]; 
[request setHTTPBody: jsonData]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"]; 

NSError *errorReturned = nil; 
NSURLResponse *theResponse =[[NSURLResponse alloc]init]; 
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned]; 

if (errorReturned) { 
    NSLog(@"Error %@",errorReturned.description); 
} 
else 
{ 
    NSError *jsonParsingError = nil; 
    NSMutableArray *arrDoctorInfo = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&jsonParsingError]; 
    NSString* newStr = [NSString stringWithUTF8String:[data bytes]]; 
    NSLog(@"Dict %@",newStr); 
} 
相關問題