Merge branch 'features/login' into develop

This commit is contained in:
GME 2018-11-25 17:27:55 +01:00
commit b4d695d5e1
4 changed files with 85 additions and 18 deletions

View file

@ -9,7 +9,9 @@ import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.monnthic.monnethicmobile.R;
import org.json.JSONObject;
import monnethic.mobile.restApi.Config;
@ -68,37 +70,45 @@ public class LoginActivity extends AppCompatActivity {
}
//AsyncTask for login
private class UserLoggerTask extends AsyncTask<String,String,String> {
private class UserLoggerTask extends AsyncTask<String,String,JSONObject> {
Context mContext;
private UserLoggerTask(final Context context){
mContext = context;
}
@Override
protected void onPostExecute(String result) {
protected void onPostExecute(JSONObject result) {
try{
if(result!=null){
if(result.getString("response").equals("Ok")){
Intent accountIntent = new Intent(LoginActivity.this, UserAccountActivity.class);
accountIntent.putExtra("userHash", result);
accountIntent.putExtra("userHash", result.getString("userHash"));
LoginActivity.this.startActivity(accountIntent);
finish();
}else {
Toast.makeText(mContext, "Wrong authentication", Toast.LENGTH_SHORT).show();
Toast.makeText(mContext, result.getString("response"), Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(mContext, "AN ERROR OCCURED", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
e.printStackTrace();
}
}
@Override
protected String doInBackground(String... params) {
protected JSONObject doInBackground(String... params) {
try{
String url = Config.USER_GET;
String email = params[1];
String password = params[2];
String[] paramsList = {email,password};
String url = Config.USER_LOGIN;
//String email = params[0];
//String password = params[1];
String[] paramsList = {params[0],params[1]};
HttpCallHandler httpCallHandler = new HttpCallHandler();
String response = httpCallHandler.executeGetHttp(url,paramsList);
JSONObject json = new JSONObject(response);
String response = httpCallHandler.executePostHttp(url,paramsList);
return json.getString("user_hash");
return new JSONObject(response);
}catch (Exception e){
e.printStackTrace();
return null;

View file

@ -2,7 +2,7 @@ package monnethic.mobile.restApi;
public class Config {
//BASE
static private String BASE_URL = "http://10.0.2.2:8083/";
static private String BASE_URL = "http://10.0.2.2:10053/";
//DATABASE
static private String BASE_URL_USER = BASE_URL+"api/rest/user/";

View file

@ -1,6 +1,9 @@
package monnethic.mobile.restApi;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@ -55,4 +58,58 @@ public class HttpCallHandler {
}
}
}
public String executePostHttp(String urlParam, String[] params){
InputStream inputStream;
BufferedReader bufferedReader = null;
HttpURLConnection urlConnection = null;
try{
System.out.println("executePostHttp");
URL url = new URL(urlParam);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
connection.setRequestProperty("Accept","application/json");
connection.setDoOutput(true);
connection.setDoInput(true);
System.out.println("coucou");
JSONObject jsonParam = new JSONObject();
jsonParam.put("email",params[0]);
jsonParam.put("password",params[1]);
System.out.println("jsonParam : "+jsonParam.toString());
DataOutputStream os = new DataOutputStream(connection.getOutputStream());
os.writeBytes(jsonParam.toString());
os.flush();
os.close();
System.out.println("STATUS "+String.valueOf(connection.getResponseCode()));
System.out.println("MSG "+connection.getResponseMessage());
connection.disconnect();
return null;
}catch (Exception e){
e.printStackTrace();
return null;
} finally {
if(urlConnection != null){
urlConnection.disconnect();
}
if(bufferedReader != null){
try {
bufferedReader.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
}