保存职位从Java / Android应用程序与色器件认证轨服务器 [英] Save posts to rails server from Java/ Android application with devise authentication

查看:137
本文介绍了保存职位从Java / Android应用程序与色器件认证轨服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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.

我发现这个(<一href="http://stackoverflow.com/questions/4294229/what-can-i-use-to-allow-an-android-app-to-communicate-with-a-rails-app">link)这显示了基本的想法,但不是实际的code ..

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

美中不足的是,用户需要登录与色器件,所以他们只看到自己的数据,而不是我或你的​​!

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.

当您连接到服务器。每一个请求将是服务器的web服务调用。你可以在头中的Base64 EN codeD形式发送身份验证。这样每一个请求被解析在服务器和凭证可以脱codeD和服务响应之前验证。

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.

检查<一href="http://stackoverflow.com/questions/8800703/api-for-android-os-in-ruby-on-rails/8801934#8801934">this查看详细的问题

有关使用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.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;
}

修改是用户名和密码pre组。使用像preference屏幕画面进行设置。可以参考json.org用于解析和创建JSON。是可以创建嵌套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应用程序与色器件认证轨服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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