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,7 +80,7 @@ public class LoginActivity extends AppCompatActivity {
protected void onPostExecute(JSONObject result) {
try{
if(result!=null){
if(result.getString("response").equals("Ok")){
if(result.getInt("status") == 200){
Intent accountIntent = new Intent(LoginActivity.this, UserAccountActivity.class);
accountIntent.putExtra("userHash", result.getString("userHash"));
LoginActivity.this.startActivity(accountIntent);
@ -88,7 +88,6 @@ public class LoginActivity extends AppCompatActivity {
}else{
Toast.makeText(mContext, result.getString("response"), Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(mContext, "AN ERROR OCCURED", Toast.LENGTH_SHORT).show();
}
@ -96,19 +95,13 @@ public class LoginActivity extends AppCompatActivity {
e.printStackTrace();
}
}
@Override
protected JSONObject doInBackground(String... params) {
try{
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.executePostHttp(url,paramsList);
return new JSONObject(response);
return new JSONObject(httpCallHandler.executePostHttp(url,paramsList));
}catch (Exception e){
e.printStackTrace();
return null;

View file

@ -60,64 +60,56 @@ public class HttpCallHandler {
}
public String executePostHttp(String urlParam, String[] params){
InputStream inputStream;
BufferedReader bufferedReader = null;
HttpURLConnection urlConnection = null;
HttpURLConnection connection = null;
String res = null;
try{
System.out.println("CALL EXECUTE POST HTTP");
URL url = new URL(urlParam);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
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);
JSONObject jsonParam = new JSONObject();
jsonParam.put("email",params[0]);
jsonParam.put("password",params[1]);
DataOutputStream os = new DataOutputStream(connection.getOutputStream());
os.writeBytes(jsonParam.toString());
int statusCode = connection.getResponseCode();
if(statusCode != 200){
System.out.println("Error response");
System.out.println(statusCode);
JSONObject jsonReturn = new JSONObject();
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();
}
inputStream = urlConnection.getInputStream();
if(inputStream == null){
System.out.println("Error inputStream");
}
bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
}else{
bufferedReader = new BufferedReader(new InputStreamReader(connection.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);
JSONObject jsonSuccess = new JSONObject(stringBuilder.toString());
jsonSuccess.put("status",200);
res = jsonSuccess.toString();
os.flush();
os.close();
connection.disconnect();
return null;
}
return res;
}catch (Exception e){
e.printStackTrace();
return null;
} finally {
if(urlConnection != null){
urlConnection.disconnect();
if(connection != null){
connection.disconnect();
}
if(bufferedReader != null){
try {

View file

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

View file

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