栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用devise身份验证将帖子从Java / Android应用程序保存到Rails服务器

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用devise身份验证将帖子从Java / Android应用程序保存到Rails服务器

JSON将更适合Android应用。它比XML轻巧。

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

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

对于使用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.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>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。是的,可以创建嵌套的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);


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/483715.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号