<span style="font-size:18px;">上面是定义接口,interface_show还未写好,app/controllers/interface_controller.rb
</span>
<span style="font-size:18px;"> 1 # -*- encoding : utf-8 -*-
2 class InterfaceController < ApplicationController
3
4 def create_user
5 User.create email: params['email'], encrypted_password: params['password']
6 render :text => 'ok!'
7 end
8
9 def interface_show
10 # @message =
11 render json: {
12 result: @message.map{ |message|
13 {
14 id: message.id,
15 name: message.name
16 }
17 },
18 comment: '测试用接口,属性,id ,name:'
19 }
20 end
21 end</span>
添加路由/workspace/work/rubyonrails/happystock1/config/routes.rb
<span style="font-size:18px;"> 15 resources :interface do
16 collection do
17 get :interface_show
18 get :create_user
19 post :create_user
20 end
21 end
</span>
android中添加请求:
<span style="font-size:18px;">function get_pop_stock(e) {
//取输入框里面的值
//var input_name = $.input_name.value;
//获取URL
var url = "http://127.0.0.1:3000/interface_index";
var client = Ti.Network.createHTTPClient({
onload : function(e) {
//解析Json
var jsons = JSON.parse(this.responseText);
var str = jsons.result;
//更改text的值
$.label.setText(str);
},
onerror : function(e) {
Ti.API.debug(e.error);
alert('error');
},
timeout : 5000 // in milliseconds
});
client.open("GET", url);
client.send();
}</span>