2012-01-10 10 views
1

アンドロイドアプリのCRUD機能を備えたRuby on Railsで開発するRESTful APIを提供する必要があります。私はRoRとの以前の携帯電話OS(アンドロイド/ iphone)の統合に取り組んでいません。ですから、始めてみてください。Ruby on RailsのAndroid OS用API

ありがとうございます。

答えて

2

デバイスにJsonパケットとしてデータを送信し、デバイスでjsonパケットを解析してデータにアクセスできます。 Webサービスへの呼び出しはHTTP呼び出しでなければなりません。例えば、これはクライアント用です。

のhttp:\サーバー\ metnod \ get_somedata名=何か

とサーバがこのパラメータにデータベースを照会し、JSONとしてあなたにREPONSEを送るべきか。 jsonを解析して詳細を取得します。

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; 

サーバー側では、my_controllerというコントローラにget_employeeメソッドが必要です。この方法であなたは、通常のHTTPリクエストとしてリクエストを処理し、JSONなどのように応答を送信することができますが、update_employeeなどを作成するためにjson.orgを参照してくださいdelete_employeeなどの適切なパラメータを使用してさまざまな方法を作成する必要がCRUD用

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 

/アンソロイドのjsonを解析する

+0

こんにちは@Andro、Thanxは応答しますが、それをどのようにしてルビと統合し、認証システムが必要ですか?Omniauthの仕事で工夫しますか? – Metal

+0

base64認証を使用しました。私はオムニバスを知らない。ごめんなさい – AD14