在django服务器中使用android app进行身份验证

问题描述:

我已经在django-restframework和django admin的帮助下创建了一个服务器.它是一个简单的服务器,可以响应请求,并带有json格式的音乐列表. 当我输入站点http://127.0.0.1/music/时,它将显示身份验证页面的链接.身份验证后,我可以看到所有内容,并发布音乐或获取音乐列表. 从我的终端机(ubuntu),我可以使用命令"curl -X POST http://127.0.0.1:8000/music/ -d "code=print 789" -u user:password"发布一些内容. 但是我非常新手,没有想法,我该如何从我的android应用程序中做到这一点. 我已经看到了许多有关如何使用网络服务的示例,例如这一个,但是没有人显示我如何进行身份验证. 抱歉,如果这是一个琐碎的问题,我只在该项目中工作了几天,而我没有编程知识. 有人可以帮我吗?

I have created a server with help of django-restframework and django admin. Its a simple server that response a request, with a list of musics in json format. When i enter in the site http://127.0.0.1/music/ it shows link for an authentication page. After authentication i can see all the content, and post a music or get a list of musics. From my terminal(ubuntu), i can post some content by using the comand "curl -X POST http://127.0.0.1:8000/music/ -d "code=print 789" -u user:password". However i'm very newbie and have no ideia how can i do that from my android app. I have seen a lot of examples about how consuming webservice, like this one, but no one shows how can i make authentication. Sorry if this is a trivial question, i'm only working in that project for a few days and i haven't programming knowledge. Can someone help me?

我使用

I created an api-token-auth using that tutorial. Then i did a POST by these codes:

public class ServiceHandler {

private static final String targetURL = "host/api-token-auth/";

public static String executePost(){
    UsuarioSenha usuariosenha;
    usuariosenha = new UsuarioSenha();
    usuariosenha.username="myusername";
    usuariosenha.password="mypassword";
    StringBuffer response = new StringBuffer();
    Gson gson= new Gson();
    String user_pass_json = gson.toJson(usuariosenha);


    HttpURLConnection httpConnection = null;
    try{
        //Criando a conexão
        URL tagetUrl = new URL(targetURL);
        httpConnection = (HttpURLConnection) tagetUrl.openConnection();

        httpConnection.setDoOutput(true);
        httpConnection.setRequestMethod("POST");
        httpConnection.setRequestProperty("Content-Type", "application/json");
        httpConnection.connect();


        //Enviando Request
        OutputStream outputStream = httpConnection.getOutputStream();
        outputStream.write(user_pass_json.getBytes());
        outputStream.flush();

        if (httpConnection.getResponseCode() != 200){
            return ("Failed : HTTP error code : " + httpConnection.getResponseCode());
        }

        //Recebendo Response
        InputStream is = httpConnection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));

        String line;
        while((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\r');
        }
        rd.close();
        return response.toString();

    }catch (MalformedURLException e) {
        e.printStackTrace();
        return "MalformedURLException";

    } catch (IOException e) {
        e.printStackTrace();
        return ""+httpConnection.getErrorStream ();
    }finally {

        if(httpConnection != null) {
            httpConnection.disconnect(); 
        }
    }
}
}

所以,我得到了返回字符串

So, i get return the string

{'token':'9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b'}

{ 'token' : '9944b09199c62bcf9418ad846dd0e4bbdfc6ee4b' }

就像 Django Rest Framework网站 g所示.

谢谢大家,尤其是Martol1ni!

Thank you all, Especially Martol1ni!