update login

not working yet
This commit is contained in:
GME 2018-11-25 17:27:51 +01:00
parent ca00426965
commit 58b54678c3
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.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Toast; import android.widget.Toast;
import com.example.monnthic.monnethicmobile.R; import com.example.monnthic.monnethicmobile.R;
import org.json.JSONObject; import org.json.JSONObject;
import monnethic.mobile.restApi.Config; import monnethic.mobile.restApi.Config;
@ -68,37 +70,45 @@ public class LoginActivity extends AppCompatActivity {
} }
//AsyncTask for login //AsyncTask for login
private class UserLoggerTask extends AsyncTask<String,String,String> { private class UserLoggerTask extends AsyncTask<String,String,JSONObject> {
Context mContext; Context mContext;
private UserLoggerTask(final Context context){ private UserLoggerTask(final Context context){
mContext = context; mContext = context;
} }
@Override @Override
protected void onPostExecute(String result) { protected void onPostExecute(JSONObject result) {
if(result!=null){ try{
Intent accountIntent = new Intent(LoginActivity.this, UserAccountActivity.class); if(result!=null){
accountIntent.putExtra("userHash", result); if(result.getString("response").equals("Ok")){
LoginActivity.this.startActivity(accountIntent); Intent accountIntent = new Intent(LoginActivity.this, UserAccountActivity.class);
finish(); accountIntent.putExtra("userHash", result.getString("userHash"));
}else{ LoginActivity.this.startActivity(accountIntent);
Toast.makeText(mContext, "Wrong authentication", Toast.LENGTH_SHORT).show(); finish();
}else {
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 @Override
protected String doInBackground(String... params) { protected JSONObject doInBackground(String... params) {
try{ try{
String url = Config.USER_GET; String url = Config.USER_LOGIN;
String email = params[1]; //String email = params[0];
String password = params[2]; //String password = params[1];
String[] paramsList = {email,password}; String[] paramsList = {params[0],params[1]};
HttpCallHandler httpCallHandler = new HttpCallHandler(); HttpCallHandler httpCallHandler = new HttpCallHandler();
String response = httpCallHandler.executeGetHttp(url,paramsList); String response = httpCallHandler.executePostHttp(url,paramsList);
JSONObject json = new JSONObject(response);
return json.getString("user_hash"); return new JSONObject(response);
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();
return null; return null;

View file

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

View file

@ -1,6 +1,9 @@
package monnethic.mobile.restApi; package monnethic.mobile.restApi;
import org.json.JSONObject;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; 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();
}
}
}
}
} }