使用设计身份验证将帖子从 Java/Android 应用程序保存到 Rails 服务器 [英] Save posts to rails server from Java/ Android application with devise authentication

查看:21
本文介绍了使用设计身份验证将帖子从 Java/Android 应用程序保存到 Rails 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Rails 服务器,我想要我的 Java 桌面应用程序 &android 应用程序能够与标准脚手架(新建/编辑/显示/等)进行交互,以便我可以在所有内容之间同步数据.

I have a Rails server, and I want my Java desktop application & android app to be able to interact with the standard scaffold (new/ edit/ show/ etc) so I can sync data between everything.

我发现了这个(link) 显示了基本思想,但没有显示实际代码..

I found this (link) which shows the basic idea but not the actual code..

问题是用户需要使用 devise 登录,所以他们只能看到他们的数据,而不是我的或你的!

The catch is that the user needs to be logged in with devise, so they only see their data, not mine or yours!

请帮我解决这个问题.

推荐答案

JSON 将更适合 Android 应用程序.它比 XML 轻量级.

JSON will better for android apps. Its lightweight than XML.

当您连接到服务器时.每个请求都会被 webservice 调用到服务器.您可以以 Base64 编码形式在标头中发送身份验证.因此,每个请求都在服务器中进行解析,并且可以在提供响应之前对凭据进行解码和验证.

when you are connecting to a server. each request will be webservice call to the server. you can send the authentication in the header in Base64 encoded form. so each request is parsed in the server and the credentials can be decoded and authenticated before serving the response.

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

To identify the device you can send the devices IME number. you can have a table to keep track of the devices that log into your server.

检查这个问题的详细信息

客户端使用 json 进行 base64 认证.我还没有完成 xml.

For the base64 authentication in the client side using json. i haven't done with 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.comcontrollergetuser"


    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>
"+jsonObjRecv.toString()+"
</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 可以创建嵌套的 jsons.

Edit yes username and password pre set. use a screen like preference screen to set it. can refer json.org for parsing and creating json. yes can create nested 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);

这篇关于使用设计身份验证将帖子从 Java/Android 应用程序保存到 Rails 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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