[英]Rest API Web Service - iOS
這是我第一次在iOS中使用網絡服務。 REST是我的首選,我使用代碼點火器來形成它。
我有我的控制器:
require APPPATH.'/libraries/REST_Controller.php';
class Sample extends REST_Controller
{
function example_get()
{
$this->load->helper('arh');
$this->load->model('users');
$users_array = array();
$users = $this->users->get_all_users();
foreach($users as $user){
$new_array = array(
'id'=>$user->id ,
'name'=>$user->name,
'age'=>$user->age,
);
array_push( $users_array, $new_array);
}
$data['users'] = $users_array;
if($data)
{
$this->response($data, 200);
}
}
function user_put()
{
$this->load->model('users');
$this->users->insertAUser();
$message = array('message' => 'ADDED!');
$this->response($message, 200);
}
}
,使用我的網絡瀏覽器訪問URL http://localhost:8888/restApi/index.php/sample/example/format/json
確實可以正常工作,並提供以下輸出:
{"users":[{"id":"1","name":"Porcopio","age":"99"},{"id":"2","name":"Name1","age":"24"},{"id":"3","name":"Porcopio","age":"99"},{"id":"4","name":"Porcopio","age":"99"},{"id":"5","name":"Luna","age":"99"}]}
,這在我的應用程序中使用RestKit的RKRequest給了我很大的輸出。
問題在於put方法。 這個網址:
http://localhost:8888/restApi/index.php/sample/user
總是給我這樣的錯誤:
此XML文件似乎沒有與之關聯的任何樣式信息。 文檔樹如下所示。
<xml>
<status/>
<error>Unknown method.</error>
這是我的用戶模型
<?php
class Users extends CI_Model {
function __construct()
{
parent::__construct();
}
function get_all_users()
{
$this->load->database();
$query = $this->db->get('users');
return $query->result();
}
function insertAUser(){
$this->load->database();
$data = array('name'=> "Sample Name", 'age'=>"99");
$this->db->insert('users', $data);
}
}
?>
我的_put方法的解決方法是什么,為什么我什么都不插入? 謝謝大家!
除非將方法設置為PUT
或POST
,否則您的Web服務器不會將其視為此類。 在瀏覽器欄中輸入URL時,幾乎總是GET請求。 您可以嘗試使用curl
curl -X POST -d @filename http://your.url.path/whatever
另一個鏈接是: https : //superuser.com/questions/149329/what-is-the-curl-command-line-syntax-to-do-a-post-request
因此,您應該能夠類似地執行PUT(也許沒有數據)。 雖然不太確定這是否應該用iOS標記:)
我通過在REST Client中使用Firefox插件來解決此問題。 我只需要指出標題和正文即可進行放置和發布工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.