使用JSON的Andr​​oid认证 [英] Android authentication using JSON

查看:132
本文介绍了使用JSON的Andr​​oid认证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python / Django的服务器是一个Web服务API。结果
我建立一个Android应用程序将与API沟通和验证用户身份,并让他们尽一切拉/从服务器推。

我的麻烦是具有与服务器的特定通信。目前我使用我的WiFi网络,并运行像这样蟒蛇manage.py的runserver 192.168.1.3:8000 服务器,以便它是提供给我的局域网上的任何试验装置。

这个API是这么写返回 HTTP状态消息与每一个反应,这样我就可以解析JSON回复之前告诉请求的成功或失败。

在我的Andr​​oid方面,我已经使用 HttpURLConnection类,因为它是我使用的getHeaderField(空)方法从响应挑的 http状态信息的。我得到一个状态消息的 200成功] 的时候我'平'我的服务器 - 这是一个概念验证排序的

我的真正的问题是认证。我的API需要我送它一个JSON数据,并返回一个JSON响应[在头部的HTTP状态消息。结果
我似乎无法弄清楚如何做到这一点。我周围的interwebs看到JSON动作仅仅采摘,或张贴。结果
我想知道我怎么可以张贴,拿起从服务器的响应。

额外信息结果
- 服务器支持HEAD和GET和OPTIONS结果。
- 假设服务器主是192.168.1.3,用户登录/注册将是192.168.1.3/user,事件将是192.168.1.3/events等..结果
- 是我得找出一个解决方案最接近的,但并不完全。

从AsyncTask的 code

 受保护的JSONObject doInBackground(字符串... PARAMS){
publishProgress(真);/ *创建一个新的HttpClient和后头* /
JSONObject的结果= NULL;
HttpClient的HttpClient的=新DefaultHttpClient();
尝试{    网址URL =新的URL(cons.PROTOCOL,cons.SERVER,cons.PORT,/用户);
HttpPost httppost =新HttpPost(url.toURI());
HTT presponse响应= NULL;    / *添加您的数据* /
JSONObject的J1 =新的JSONObject();
JSONObject的JSON =新的JSONObject();
j1.put(用户名,测试);
j1.put(电子邮件,test@test.com);
j1.put(密码,密码);
j1.put(FIRST_NAME,约翰);
j1.put(姓氏,李四);
json.put(用户,J1);
json.put(MOBILE_NUMBER,256774622240);
StringEntity本身=新StringEntity(json.toString());
se.setContentType(新BasicHeader(HTTP.CONTENT_TYPE,应用/ JSON));
httppost.setEntity(SE);    / *执行HTTP POST请求* /
    响应= httpclient.execute(httppost);        Log.i(爵士,这是活着!!!!!);
        Log.i(爵士,response.getStatusLine()的toString());}赶上(ClientProtocolException E){
    / * TODO自动生成catch块* /
}赶上(IOException异常五){
    // TODO自动生成catch块
}赶上(的URISyntaxException E){
    // TODO自动生成catch块
    e.printStackTrace();
}赶上(JSONException E){
    // TODO自动生成catch块
    e.printStackTrace();
}返回结果;
}


解决方案

好了,所以我现在回答我的问题:D

问题是与路径 URL字符串变量。结果
这是在此基础上文件的URL构造函数之一的格式。结果
URL(协议字符串,字符串主机,端口INT,字符串文件)

由于我张贴在 JSON /用户路径,这是一个我插入构造为目录。结果
所以,我的网址,形成像这样:结果
网​​址URL =新的URL(HTTP,cons.SERVER,cons.PORT,/用户/);

我在一开始的错误是使用 /用户而不是 /用户/
但除此之外,在 URL结构和连接都是正常的。

I have a Python/Django server that is the API for a web service.
I'm building an Android application that will communicate with the API and authenticate users, and enable them do all pulls/pushes from the server.

My trouble is with the particular communication with the server. Currently I use my WiFi network, and run the server like so python manage.py runserver 192.168.1.3:8000 so that it is available to any test device on my LAN.

The API is written so it returns http status messages with every response, so that I can tell the success or failure of a request before parsing the JSON reply.

On my Android side, I have used HttpURLConnection because it has the getHeaderField(null) method that I use to pick the http status message from the response. I get a status message 200 [success] when I 'ping' my server - this is a sort-of proof of concept.

My real issue is authentication. My API requires I send it a JSON with data, and it returns a JSON response [with an http status message in the head].
I can't seem to figure out how to do this. The JSON action I've seen around the interwebs are merely picking, or posting.
I am wondering how I can POST and pick up a response from the server.

Extra information
- Server supports HEAD and GET and OPTIONS.
- Assuming server home is 192.168.1.3, user login/register would be in 192.168.1.3/user, events would be in 192.168.1.3/events and so on..
- This was the closest I got to figuring out a solution, but not quite..

CODE from the AsyncTask

protected JSONObject doInBackground(String... params)  {
publishProgress(true);

/*Create a new HttpClient and Post Header*/
JSONObject result=null;
HttpClient httpclient = new DefaultHttpClient();
try {

    URL url = new URL(cons.PROTOCOL,cons.SERVER,cons.PORT,"/user");
HttpPost httppost = new HttpPost(url.toURI());
HttpResponse response =null;

    /*Add your data*/
JSONObject j1=new JSONObject();
JSONObject json=new JSONObject();
j1.put("username", "test");
j1.put("email","test@test.com");
j1.put("password","password");
j1.put("first_name","John");
j1.put("last_name","Doe");
json.put("user",j1);
json.put("mobile_number","256774622240");
StringEntity se = new StringEntity( json.toString());  
se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);

    /*Execute HTTP Post Request*/
    response= httpclient.execute(httppost);

        Log.i("jazz","It's ALIVE!!!!!");
        Log.i("jazz",response.getStatusLine().toString());

} catch (ClientProtocolException e) {
    /* TODO Auto-generated catch block*/
} catch (IOException e) {
    // TODO Auto-generated catch block
} catch (URISyntaxException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}



return result;
}

解决方案

Okay, so I'm now answering my own question :D

The issue was with the path variable in the URL string.
This is the format of one of the URL constructors based on this document.
URL(String protocol, String host, int port, String file)

Since I am posting the JSON to the /user path, that's the one I insert into the constructor as the directory.
So, my URL was formed like so:
URL url= new URL("http",cons.SERVER,cons.PORT,"/user/");

My mistake in the beginning was using /user instead of /user/ but other than that, the URL structure and connections are all alright.

这篇关于使用JSON的Andr​​oid认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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