我有一个 Rails 服务器,我希望我的 Java 桌面应用程序和 Android 应用程序能够与标准脚手架(新建/编辑/显示/等)交互,以便我可以在所有内容之间同步数据。
我发现这个 ( link ) 显示了基本思想但没有显示实际代码..
问题是用户需要使用 devise 登录,所以他们只能看到他们的数据,看不到我的或你的!
请帮我解决这个问题。
最佳答案
JSON 更适用于 Android 应用程序。它比 XML 轻量级。
当您连接到服务器时。每个请求都会被 webservice 调用到服务器。您可以以 Base64 编码形式在 header 中发送身份验证。因此每个请求都在服务器中进行解析,并且可以在提供响应之前对凭据进行解码和验证。
要识别设备,您可以发送设备 IME 编号。你可以有一个表来跟踪登录到你的服务器的设备。
检查 this详细问题
客户端使用json进行base64认证。我还没有完成 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,"<jsonobject>\n"+jsonObjRecv.toString()+"\n</jsonobject>");
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。 yes 可以创建嵌套的 json。
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);
关于java - 使用设计身份验证将帖子从 Java/Android 应用程序保存到 Rails 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8950279/