在Ruby on Rails的针对Android操作系统的API [英] API for Android OS in ruby on rails
问题描述
我需要提供REST风格的API进行开发的Ruby on Rails的带有CRUD功能的Android应用程序。我还没有与手机操作系统(安卓/ iphone),与早先的回报率集成。所以,请大家帮我入门。
I need to provide an RESTful api to be developed in ruby on rails with CRUD features for an android app. I haven't worked with mobile phone OS(android/iphone) integration with RoR earlier. So, please help me getting started.
在此先感谢。
推荐答案
您可以将数据为JSON数据包发送到设备,在设备解析JSON数据包和访问数据。你的电话到Web服务应该是一个HTTP调用,例如这是给客户端。
you can send the data as Json packets to the device and in device parse the json packets and access the data. your calls to webservice should be a http call eg this is for the client.
HTTP:?\服务器\方法An Iteraitve \ get_somedata名=事情
http:\server\metnod\get_somedata?name=something
和服务器应该在数据库中查询此参数并发送您的效应初探为JSON。解析JSON,让您的详细信息。
and the server should query the database for this parameter and send you the reponse as Json. parse json and get your details.
String url = "http:\\example.com\mycontroller\get_employee?id=2"
HttpPost httpPostRequest = new HttpPost(url);
StringEntity se;
se = new StringEntity(jsonObjSend.toString());
httpPostRequest.setEntity(se);
httpPostRequest.setHeader("Authorization", usercredential);
httpPostRequest.setHeader("Accept", "application/json");
httpPostRequest.setHeader("Content-type", "application/json");
long t = System.currentTimeMillis();
response = (HttpResponse) httpclient.execute(httpPostRequest);
Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]");
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
instream = new GZIPInputStream(instream);
}
// convert content stream to a String
String resultString= convertStreamToString(instream);
Log.v(null, "resultString "+resultString);
instream.close();
// Transform the String into a JSONObject
if(resultString!=null){
jsonObjRecv = new JSONObject(resultString);
}
// Raw DEBUG output of our received JSON object:
Log.i(TAG,"<jsonobject>\n"+jsonObjRecv.toString()+"\n</jsonobject>");
return jsonObjRecv;
在服务器端,你应该有一个控制器称为一个myController的方法get_employee。在该方法中,你可以处理该请求作为一个正常的HTTP请求和发送响应,JSON,例如
In the server side you should have a get_employee method in a controller called mycontroller. in the method you can process the request as a normal http request and send the response as json eg
employee = Employee.find_by_id(params[:id])
@js_response = ActiveSupport::JSON.encode(employee)
respond_to do |format|
format.json { render :json => @js_response}
format.html
end
有关CRUD你需要创建不同的方法与像delete_employee合适的参数,update_employee等参考json.org开创的Android /解析JSON
for CRUD you need to create different methods with appropriate parameters like delete_employee, update_employee etc. refer json.org to create/parse json in android
这篇关于在Ruby on Rails的针对Android操作系统的API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!