Save posts to rails server from Java/ Android application with devise authentication

后端 未结 1 1596
失恋的感觉
失恋的感觉 2021-01-16 06:33

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 b

相关标签:
1条回答
  • 2021-01-16 07:05

    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);
    
    0 讨论(0)
提交回复
热议问题