Ruby on Rails 中 Android 操作系统的 API [英] API for Android OS in ruby on rails

查看:18
本文介绍了Ruby on Rails 中 Android 操作系统的 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要提供一个 RESTful api,以便在 ruby​​ on rails 中开发具有 CRUD 功能的 Android 应用程序.我之前没有使用过与 RoR 集成的手机操作系统(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 数据包并访问数据.您对网络服务的调用应该是 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:\server\metnod\get_somedata?name=something

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆