使用设计认证将post从Java / Android应用程序保存到rails服务器

我有一个Rails服务器,我希望我的Java桌面应用程序和Android应用程序能够与标准脚手架(new / edit / show / etc)进行交互,这样我就可以在所有内容之间同步数据。

我发现这个( 链接 )显示了基本的想法,但没有显示实际的代码。

问题在于用户需要使用设备登录,因此他们只能看到他们的数据,而不是我的或您的数据!

请帮我解决这个问题。

JSON将更好的Android应用程序。 它比XML轻。

当您连接到服务器时。 每个请求都将webservice调用到服务器。 您可以在Base64编码forms的标头中发送身份validation。 因此,在服务器中解析每个请求,并且在提供响应之前可以解密和validation凭证。

要识别设备,您可以发送设备IME编号。 您可以拥有一个表来跟踪登录到您服务器的设备。

检查这个问题的细节

对于使用json的客户端中的base64身份validation。 我还没有使用xml。

 public static JSONObject SendHttpPost(Context context, JSONObject jsonObjSend) { mPrefs = AppConfig.getPreferences(context); String username = mPrefs.getString("UserName",""); String password = mPrefs.getString("Password",""); String host = mPrefs.getString("URL",""); String port = mPrefs.getString("Port",""); String url = "http:\\myapp.com\controller\getuser" HttpResponse response = null ; JSONObject jsonObjRecv =null; try { String usercredential = Utility.getB64Auth(username, password); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost httpPostRequest = new HttpPost(url); StringEntity se; se = new StringEntity(jsonObjSend.toString()); // Set HTTP parameters 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]"); //Get hold of the response entity (-> the data): HttpEntity entity = response.getEntity(); if (entity != null) { // Read the content stream 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,"\n"+jsonObjRecv.toString()+"\n"); return jsonObjRecv; } } catch(SocketException se){ se.printStackTrace(); }catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } return null; } 

编辑是用户名和密码预设。 使用偏好屏幕之类的屏幕进行设置。 可以参考json.org来解析和创建json。 是的可以创建嵌套的jsons。

 JSONObject body = new JSONObject(); JSONObject note = new JSONObject(); JSONObject commit = new JSONObject(); note.put("value", test2); commit.put("create", note); body.put("note", note); body.put("commit", commit);