0
I am developing a mobile application that will consume services from my server, developed in the Play Framework. I have already created the login service as below:
public class Services extends Controller {
public static void login(String login, String senha) throws Exception {
Agente agente = new Agente();
agente.login = login;
agente.senha = senha;
if (agente.autenticar()) {
agente = Agente.find("login = ?", agente.login).first();
Gson json = new Gson();
renderJSON(json.toJson(agente));
} else {
String mensagem = "Usuário ou senha incorreto";
JsonObject j = new JsonObject();
j.addProperty("Erro", 404);
j.addProperty("msg", mensagem);
renderJSON(j);
}
}
}
My controller’s route in Play: GET /Services/login/{login}/{password} Services.login
The Androidmanifestxml file:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".PrincipalActivity" />
<activity android:name=".FormPalhetasActivity" />
<activity android:name=".ColetarActivity" />
<activity android:name=".SobreActivity"></activity>
</application>
My dependency:compile 'com.android.volley:volley:1.1.0'
and the class responsible for my login on android is:
package com.diego.agentesendemias;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.squareup.okhttp.OkHttpClient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import static android.widget.Toast.LENGTH_LONG;
public class MainActivity extends AppCompatActivity {
public static final String LOGIN_URL = "http://192.168.0.108:9000/Services/login";
public static final String KEY_USERNAME = "username";
public static final String KEY_PASSWORD = "password";
private String username;
private String password;
private EditText editTextUsername;
private EditText editTextPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
editTextUsername = (EditText) findViewById(R.id.matricula);
editTextPassword = (EditText) findViewById(R.id.senha);
Button logar = (Button) findViewById( R.id.logar );
logar.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
userLogin();
}
} );
}
public void userLogin(){
username = editTextUsername.getText().toString().trim();
password = editTextPassword.getText().toString().trim();
StringRequest stringRequest = new StringRequest( Request.Method.POST, LOGIN_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.trim().equals( 200 )) {
Intent intent = new Intent(MainActivity.this, PrincipalActivity.class);
intent.putExtra(KEY_USERNAME, username);
startActivity(intent);
} else {
Toast.makeText( MainActivity.this, response, Toast.LENGTH_LONG ).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText( MainActivity.this, error.toString(), Toast.LENGTH_LONG ).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError{
Map<String, String> map = new HashMap<>();
map.put(KEY_USERNAME, username);
map.put(KEY_PASSWORD, password);
return map;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
However, when running my application and trying to log in with a registration and password already previously registered on my server, it returns the error: Volley: [29824] Basicnetwork.performRequest: Unexpected Response code 500 for http://192.168. 0.108:9000/Services/login
Someone can help me, I’m learning now to consume services.
Could you put in your androidmanifest.xml for me to take a look at?
– Grupo CDS Informática
Ready @Grupocdsinformática, added
– Carlos Diego
Usually error 500 is some problem with the server. Try to make the call using Postman, to see if it is returning and logging in correctly.
– Grupo CDS Informática