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?
Add a comment
|
2 Answers
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' }
just like the Django Rest Framework siteg shows.
Thank you all, Especially Martol1ni!
Comments
I would look into https://github.com/GetBlimp/django-rest-framework-jwt which gives you an authentication token, then you can simply send post requests from your android app and include the username and password.
1 Comment
Filipe Dias
Ok, I have implemented the api-token-auth through the URI localhost:8000/api-token-auth, just like you showed me. My problem now its how to use the HttpURLConnection by java.net (i think that's the right one) on my android app. I have my class UserPass { String user String password } So I use Gson to convert a UserPass object on to a Json String. How can i post that Json String to recover the token from the /api-token-auth/?