1
我需要提供一個REST風格的API,可以在帶有CRUD功能的紅寶石上開發,適用於Android應用程序。我之前沒有與手機操作系統(android/iphone)集成RoR。所以,請幫助我開始。Ruby on Rails中用於Android操作系統的API
在此先感謝。
我需要提供一個REST風格的API,可以在帶有CRUD功能的紅寶石上開發,適用於Android應用程序。我之前沒有與手機操作系統(android/iphone)集成RoR。所以,請幫助我開始。Ruby on Rails中用於Android操作系統的API
在此先感謝。
您可以將數據作爲Json數據包發送到設備,並在設備中解析json數據包並訪問數據。你對web服務的調用應該是一個http調用,例如這是給客戶端的。
HTTP:\服務器\方法An Iteraitve \ get_somedata名=東西
和服務器應該在數據庫中查詢此參數併發送給您的效應初探爲JSON?解析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;
在服務器端,您應該在名爲mycontroller的控制器中使用get_employee方法。在該方法可以處理該請求作爲一個正常的HTTP請求和發送響應作爲JSON例如
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來創建不同的方法/解析JSON在Android
嗨@Andro,Thanx的響應,但我應該如何將它與紅寶石集成,我需要一個授權系統,將設計與Omniauth工作? – Metal 2012-01-11 05:13:54
我們使用了base64認證。我沒有意識到omniauth。抱歉 – AD14 2012-01-11 15:01:28