使用JSON的Android身份验证 [英] Android authentication using JSON
问题描述
我正在构建一个Android应用程序,它将与API进行通信并对用户进行身份验证,并使其能够执行所有拉/从服务器推送。
我的问题是与服务器的特定通信。目前,我使用我的WiFi网络,并运行服务器像 python manage.py runserver 192.168.1.3:8000
,以便它可用于我的LAN上的任何测试设备。 / p>
API被写入,因此每个响应都返回 http状态消息,以便在解析请求之前告诉成功或失败JSON回复
在我的Android端,我使用了 HttpURLConnection
,因为它有 getHeaderField )
方法,我用来从响应中选择 http状态消息。当我'ping'我的服务器时,我得到一个状态信息 200 [成功] - 这是一种概念证明。
我的真正的问题是认证。我的API需要我发送一个带有数据的JSON,并返回一个JSON响应[在头部有一个http状态消息]。
我似乎无法弄清楚如何做到这一点。我在interwebs周围看到的JSON操作只是选择或发布。
我想知道如何POST,并从服务器上提取响应。
额外的信息
- 服务器支持HEAD和GET和OPTIONS。
- 假设服务器的主目录是192.168.1.3,用户登录/注册将在192.168.1.3/user中,事件将在192.168.1.3/events等等。
- 这是最接近我找到一个解决方案,但不完整。
AsyncTask中的代码
protected JSONObject doInBackground(String ... params){
publishProgress(true);
/ *创建一个新的HttpClient和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;
/ *添加您的数据* /
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);
/ *执行HTTP Post Request * /
response = httpclient.execute(httppost);
Log.i(jazz,它是ALIVE !!!!!);
Log.i(jazz,response.getStatusLine()。toString());
} catch(ClientProtocolException e){
/ * TODO自动生成的catch块* /
} catch(IOException e){
// TODO自动生成catch块
} catch(URISyntaxException e){
// TODO自动生成的catch块
e.printStackTrace();
} catch(JSONException e){
// TODO自动生成的catch块
e.printStackTrace();
}
返回结果;
}
现在回答我自己的问题:D
问题是与路径
变量在 URL字符串
。
这是基于这个文档。
URL(String protocol,String host,int port,String file)
由于我将 JSON 发布到 / user
路径,所以一个我作为目录插入到构造函数中。
所以我的URL形成如下:
URL url = new URL(http,cons.SERVER,cons.PORT,/ user /);
我的开始时的错误是使用 / user
而不是 / user /
,但除此之外, strong> 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的Android身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!