Setup login api connection

Not yet tested
This commit is contained in:
GME 2018-11-24 12:52:47 +01:00
parent 878c071d4d
commit ca00426965
8 changed files with 141 additions and 21 deletions

View file

@ -22,6 +22,7 @@ public class User {
private String user_hash;
//Constructors
public User(){}
public User(String name, String firstname, String email, String password) {
this.name = name;
this.firstname = firstname;

View file

@ -9,10 +9,6 @@ import android.widget.Button;
import com.example.monnthic.monnethicmobile.R;
public class HomepageActivity extends AppCompatActivity {
//TODO MOVE TO LOGIN ACTIVITY
private static final String[] DUMMY_CREDENTIALS = new String[]{
"dummy:test"
};
@Override
protected void onCreate(Bundle savedInstanceState) {

View file

@ -1,19 +1,22 @@
package monnethic.mobile.homepage;
import android.content.Context;
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.example.monnthic.monnethicmobile.R;
import org.json.JSONObject;
import monnethic.mobile.restApi.Config;
import monnethic.mobile.restApi.HttpCallHandler;
import monnethic.mobile.user.UserAccountActivity;
public class LoginActivity extends AppCompatActivity {
private EditText email;
private EditText password;
@ -56,17 +59,52 @@ public class LoginActivity extends AppCompatActivity {
Toast.makeText(this, "You did not enter your password", Toast.LENGTH_SHORT).show();
} else {
if(InputController.checkUser(email.getText().toString(),password.getText().toString())){
launchWalletActivity(1);
String[] params = {email.getText().toString(),password.getText().toString()};
new UserLoggerTask(this).execute(params);
}else{
Toast.makeText(this, "Wrong authentication", Toast.LENGTH_SHORT).show();
}
}
}
public void launchWalletActivity(int ldapId){
//LAUNCH APP WALLET WITH ID USER
Intent accountIntent = new Intent(LoginActivity.this, UserAccountActivity.class);
accountIntent.putExtra("idUser", ldapId);
LoginActivity.this.startActivity(accountIntent);
finish();
//AsyncTask for login
private class UserLoggerTask extends AsyncTask<String,String,String> {
Context mContext;
private UserLoggerTask(final Context context){
mContext = context;
}
@Override
protected void onPostExecute(String result) {
if(result!=null){
Intent accountIntent = new Intent(LoginActivity.this, UserAccountActivity.class);
accountIntent.putExtra("userHash", result);
LoginActivity.this.startActivity(accountIntent);
finish();
}else{
Toast.makeText(mContext, "Wrong authentication", Toast.LENGTH_SHORT).show();
}
}
@Override
protected String doInBackground(String... params) {
try{
String url = Config.USER_GET;
String email = params[1];
String password = params[2];
String[] paramsList = {email,password};
HttpCallHandler httpCallHandler = new HttpCallHandler();
String response = httpCallHandler.executeGetHttp(url,paramsList);
JSONObject json = new JSONObject(response);
return json.getString("user_hash");
}catch (Exception e){
e.printStackTrace();
return null;
}
}
}
}

View file

@ -14,7 +14,6 @@ import monnethic.mobile.database.User;
import monnethic.mobile.user.UserAccountActivity;
public class RegisterActivity extends AppCompatActivity {
private EditText name;
private EditText firstname;
private EditText email;

View file

@ -1,5 +1,28 @@
package monnethic.mobile.restApi;
public class Config {
static public String URL_BALANCE = "http://10.0.2.2:8083/balance?name=";
//BASE
static private String BASE_URL = "http://10.0.2.2:8083/";
//DATABASE
static private String BASE_URL_USER = BASE_URL+"api/rest/user/";
static private String BASE_URL_TRANSACTION = BASE_URL+"api/rest/transaction/";
//USER
static public String USER_LOGIN = BASE_URL_USER+"login";
static public String USER_REGISTER = BASE_URL_USER+"save";
static public String USER_GET = BASE_URL_USER+"get";
//TRANSACTION
static public String TRANSACTION_GET = BASE_URL_TRANSACTION+"get";
static public String TRANSACTION_LATEST = BASE_URL_TRANSACTION+"getLatest";
static public String TRANSACTION_ALL = BASE_URL_TRANSACTION+"getAll";
static public String TRANSACTION_SENT = BASE_URL_TRANSACTION+"sent";
static public String TRANSACTION_RECEIVED = BASE_URL_TRANSACTION+"received";
static public String TRANSACTION_SAVE = BASE_URL_TRANSACTION+"save";
//BLOCKCHAIN
static private String BASE_URL_QUERY = BASE_URL+"/api/rest/query/";
static public String QUERY_BALANCE = BASE_URL_QUERY+"/balance?name=";
}

View file

@ -0,0 +1,58 @@
package monnethic.mobile.restApi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpCallHandler {
public String executeGetHttp(String urlParam, String[] params){
InputStream inputStream;
BufferedReader bufferedReader = null;
HttpURLConnection urlConnection = null;
try{
URL url = new URL(urlParam);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
int response = urlConnection.getResponseCode();
if(response != 200){
System.out.println("Error response");
}
inputStream = urlConnection.getInputStream();
if(inputStream == null){
System.out.println("Error inputStream");
}
bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine())!=null){
stringBuilder.append(line);
}
return stringBuilder.toString();
}catch (Exception e){
e.printStackTrace();
return null;
} finally {
if(urlConnection != null){
urlConnection.disconnect();
}
if(bufferedReader != null){
try {
bufferedReader.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
}

View file

@ -17,6 +17,7 @@ import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import monnethic.mobile.database.User;
import monnethic.mobile.restApi.Config;
import monnethic.mobile.transaction.MakePayementActivity;
import monnethic.mobile.transaction.ReceivePayementActivity;
@ -24,7 +25,7 @@ import monnethic.mobile.transaction.ReceivePayementActivity;
public class UserAccountActivity extends AppCompatActivity {
private TextView balance;
private String strUrl = Config.URL_BALANCE+"b";
private String strUrl = Config.QUERY_BALANCE+"b";
private String result;
@Override
@ -80,7 +81,7 @@ public class UserAccountActivity extends AppCompatActivity {
}
public void refreshBalance(String userHash){
String url = Config.URL_BALANCE+userHash;
String url = Config.QUERY_BALANCE+userHash;
new GetUserBalanceTask().execute(url);
}
@ -137,6 +138,10 @@ public class UserAccountActivity extends AppCompatActivity {
} catch (Exception e){
System.out.println("Exception : "+e);
} finally {
if(conn != null){
conn.disconnect();
}
}
return result;
}

View file

@ -32,7 +32,7 @@
<Button
android:id="@+id/buttonPayement"
android:layout_width="150dp"
android:layout_width="200dp"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
@ -41,7 +41,7 @@
<Button
android:id="@+id/buttonReceive"
android:layout_width="150dp"
android:layout_width="200dp"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
@ -50,7 +50,7 @@
<Button
android:id="@+id/buttonRefreshBalance"
android:layout_width="150dp"
android:layout_width="200dp"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"