且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用JSON的Android身份验证

更新时间:2023-12-03 16:36:10

现在回答我自己的问题: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.