1

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.

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!

Please help me regarding this.

Community
  • 1
  • 1
Will Richardson
  • 7,780
  • 7
  • 42
  • 56
  • (a) don't use `XML` unless you must. (b) Obviously you need to differentiate authenticated users. Look into sessions. – alphazero Jan 21 '12 at 03:43

1 Answers1

2

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

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.

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.

check this question for detail

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;
}

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);
Community
  • 1
  • 1
AD14
  • 1,218
  • 19
  • 32
  • username + password would be.. devise? pre-set? ? – Will Richardson Jan 21 '12 at 21:28
  • How do I add nested items to a JSONobject eg: "note"=>{"value"=>"test 2"}, "commit"=>"Create Note"} ? – Will Richardson Jan 22 '12 at 07:24
  • So if the username+pweir are not devise ones, what does my rails app do to authenticate them..? – Will Richardson Jan 22 '12 at 23:25
  • 1
    in the rails app we get the request header and params[:Authorization] will give you the user credentials. need to do the base64 decryption in the server side also. in my app for the first time(login from device) we register the device and its user credentials. so in the next requests we can verify them. – AD14 Jan 23 '12 at 04:38
  • do you know how I would match the decrypted password from params[:Authorization] to the password stored in the database by devise? ie, how does one encrypt the password to check if they match? – Will Richardson Jan 23 '12 at 07:06
  • Ok, you can say user.valid_password? I just need to now change the authenticate_user! to a custom mix of stuff – Will Richardson Jan 23 '12 at 07:28
  • 1
    yes. we use a base64 authentication to encrypt and decrypt password. – AD14 Jan 23 '12 at 09:14
  • I think I have it. Cheers for your help. – Will Richardson Jan 23 '12 at 09:28