Setup login with api

This commit is contained in:
GME 2018-11-25 18:17:57 +01:00
parent 91889cb162
commit 19fcfd82e5
4 changed files with 45 additions and 55 deletions

View file

@ -80,15 +80,14 @@ public class LoginActivity extends AppCompatActivity {
protected void onPostExecute(JSONObject result) { protected void onPostExecute(JSONObject result) {
try{ try{
if(result!=null){ if(result!=null){
if(result.getString("response").equals("Ok")){ if(result.getInt("status") == 200){
Intent accountIntent = new Intent(LoginActivity.this, UserAccountActivity.class); Intent accountIntent = new Intent(LoginActivity.this, UserAccountActivity.class);
accountIntent.putExtra("userHash", result.getString("userHash")); accountIntent.putExtra("userHash", result.getString("userHash"));
LoginActivity.this.startActivity(accountIntent); LoginActivity.this.startActivity(accountIntent);
finish(); finish();
}else { }else{
Toast.makeText(mContext, result.getString("response"), Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, result.getString("response"), Toast.LENGTH_SHORT).show();
} }
}else{ }else{
Toast.makeText(mContext, "AN ERROR OCCURED", Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, "AN ERROR OCCURED", Toast.LENGTH_SHORT).show();
} }
@ -96,19 +95,13 @@ public class LoginActivity extends AppCompatActivity {
e.printStackTrace(); e.printStackTrace();
} }
} }
@Override @Override
protected JSONObject doInBackground(String... params) { protected JSONObject doInBackground(String... params) {
try{ try{
String url = Config.USER_LOGIN; String url = Config.USER_LOGIN;
//String email = params[0];
//String password = params[1];
String[] paramsList = {params[0],params[1]}; String[] paramsList = {params[0],params[1]};
HttpCallHandler httpCallHandler = new HttpCallHandler(); HttpCallHandler httpCallHandler = new HttpCallHandler();
String response = httpCallHandler.executePostHttp(url,paramsList); return new JSONObject(httpCallHandler.executePostHttp(url,paramsList));
return new JSONObject(response);
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();
return null; return null;

View file

@ -60,64 +60,56 @@ public class HttpCallHandler {
} }
public String executePostHttp(String urlParam, String[] params){ public String executePostHttp(String urlParam, String[] params){
InputStream inputStream;
BufferedReader bufferedReader = null; BufferedReader bufferedReader = null;
HttpURLConnection urlConnection = null; HttpURLConnection connection = null;
String res = null;
try{ try{
System.out.println("CALL EXECUTE POST HTTP");
URL url = new URL(urlParam); URL url = new URL(urlParam);
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST"); connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8"); connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
connection.setRequestProperty("Accept","application/json"); connection.setRequestProperty("Accept","application/json");
connection.setDoOutput(true); connection.setDoOutput(true);
connection.setDoInput(true); connection.setDoInput(true);
JSONObject jsonParam = new JSONObject(); JSONObject jsonParam = new JSONObject();
jsonParam.put("email",params[0]); jsonParam.put("email",params[0]);
jsonParam.put("password",params[1]); jsonParam.put("password",params[1]);
DataOutputStream os = new DataOutputStream(connection.getOutputStream()); DataOutputStream os = new DataOutputStream(connection.getOutputStream());
os.writeBytes(jsonParam.toString()); os.writeBytes(jsonParam.toString());
int statusCode = connection.getResponseCode(); int statusCode = connection.getResponseCode();
if(statusCode != 200){ if(statusCode != 200){
System.out.println("Error response"); JSONObject jsonReturn = new JSONObject();
System.out.println(statusCode); System.out.println("Response code is : "+statusCode);
if(statusCode == 404){
jsonReturn.put("status",404);
jsonReturn.put("response","Not Found");
return jsonReturn.toString();
}else if(statusCode == 403){
jsonReturn.put("status",403);
jsonReturn.put("response","Wrong Password");
return jsonReturn.toString();
}
}else{
bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine())!=null){
stringBuilder.append(line);
}
JSONObject jsonSuccess = new JSONObject(stringBuilder.toString());
jsonSuccess.put("status",200);
res = jsonSuccess.toString();
os.flush();
os.close();
} }
inputStream = urlConnection.getInputStream(); return res;
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);
}
System.out.println("String builder response : "+stringBuilder.toString());
JSONObject json = new JSONObject(stringBuilder.toString());
System.out.println("JSON : "+json);
os.flush();
os.close();
connection.disconnect();
return null;
}catch (Exception e){ }catch (Exception e){
e.printStackTrace(); e.printStackTrace();
return null; return null;
} finally { } finally {
if(urlConnection != null){ if(connection != null){
urlConnection.disconnect(); connection.disconnect();
} }
if(bufferedReader != null){ if(bufferedReader != null){
try { try {

View file

@ -32,13 +32,16 @@ public class UserAccountActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_account); setContentView(R.layout.activity_user_account);
balance = findViewById(R.id.balanceView); balance = findViewById(R.id.balanceView);
TextView userHashView = findViewById(R.id.userHash);
Button buttonPayement = findViewById(R.id.buttonPayement); Button buttonPayement = findViewById(R.id.buttonPayement);
Button buttonReceive = findViewById(R.id.buttonReceive); Button buttonReceive = findViewById(R.id.buttonReceive);
Button buttonSettings = findViewById(R.id.buttonSettings); Button buttonSettings = findViewById(R.id.buttonSettings);
Button buttonRefresh = findViewById(R.id.buttonRefreshBalance); Button buttonRefresh = findViewById(R.id.buttonRefreshBalance);
Intent intent = getIntent();
String userHash = intent.getStringExtra("userHash");
userHashView.setText(userHash);
buttonPayement.setOnClickListener(new View.OnClickListener() { buttonPayement.setOnClickListener(new View.OnClickListener() {
@Override @Override

View file

@ -36,7 +36,7 @@
android:layout_height="45dp" android:layout_height="45dp"
android:layout_alignParentBottom="true" android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:layout_marginBottom="220dp" android:layout_marginBottom="250dp"
android:text="Payement" /> android:text="Payement" />
<Button <Button
@ -45,7 +45,7 @@
android:layout_height="45dp" android:layout_height="45dp"
android:layout_alignParentBottom="true" android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:layout_marginBottom="155dp" android:layout_marginBottom="180dp"
android:text="Receive" /> android:text="Receive" />
<Button <Button
@ -54,7 +54,7 @@
android:layout_height="45dp" android:layout_height="45dp"
android:layout_alignParentBottom="true" android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:layout_marginBottom="90dp" android:layout_marginBottom="115dp"
android:text="Refresh" /> android:text="Refresh" />
<TextView <TextView
@ -73,7 +73,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentTop="true" android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" android:layout_centerHorizontal="true"
android:layout_marginTop="169dp" android:layout_marginTop="177dp"
android:text="BALANCE" android:text="BALANCE"
android:textAlignment="center" android:textAlignment="center"
android:textSize="18sp" android:textSize="18sp"
@ -81,11 +81,13 @@
<TextView <TextView
android:id="@+id/userHash" android:id="@+id/userHash"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignStart="@+id/balanceView" android:layout_alignParentStart="true"
android:layout_alignParentTop="true" android:layout_alignParentTop="true"
android:layout_marginTop="98dp" android:layout_marginStart="0dp"
android:layout_marginTop="93dp"
android:gravity="center"
android:textSize="15sp" /> android:textSize="15sp" />