From f73b9268934921644fdbffea85851e1f51967fe6 Mon Sep 17 00:00:00 2001 From: Raffaele Date: Wed, 8 Apr 2015 14:43:53 +0200 Subject: [PATCH] Passaggio a Bluemix per le API di info utente.Aggiunta LoadingBar durante il login. Procedura di Login resa asincrona per non caricare il thread principale --- .../raffaele/testapp/MainActivity.java | 36 +++++++++++++++++-- .../com/example/raffaele/testapp/User.java | 36 ++++++++++++++----- .../main/res/layout-land/activity_main.xml | 14 ++++++++ .../main/res/layout-port/activity_main.xml | 36 ++++++++++++++----- .../res/layout-sw600dp-land/activity_main.xml | 12 +++++++ .../res/layout-sw600dp-port/activity_main.xml | 14 ++++++++ 6 files changed, 128 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/example/raffaele/testapp/MainActivity.java b/app/src/main/java/com/example/raffaele/testapp/MainActivity.java index 6416b48..6ee5503 100644 --- a/app/src/main/java/com/example/raffaele/testapp/MainActivity.java +++ b/app/src/main/java/com/example/raffaele/testapp/MainActivity.java @@ -3,8 +3,10 @@ import android.content.Intent; import android.content.res.Configuration; import android.graphics.Typeface; +import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; +import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -13,6 +15,33 @@ import android.widget.Toast; public class MainActivity extends ActionBarActivity { + //subclass for login execution + private class LoginTask extends AsyncTask { + protected User doInBackground(User... utenti) { + User u=utenti[0]; + boolean connected = u.connetti(); + if(connected) + return u; + return null; + } + + protected void onPostExecute(User utente) { + + if(utente!=null){ + Intent i = new Intent(getApplicationContext(), Welcome_student.class); + Bundle extras=new Bundle(); + //passo l'oggetto user alla prossima view + extras.putParcelable("utentec", utente); + i.putExtras(extras); + findViewById(R.id.loadBar).setVisibility(View.INVISIBLE); + startActivity(i); + } + else { + findViewById(R.id.loadBar).setVisibility(View.INVISIBLE); + Toast.makeText(getApplicationContext(), "Credenziali non valide", Toast.LENGTH_SHORT).show(); + } + } + } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -57,8 +86,10 @@ public void onStartClick(View v) { String user = ((EditText) findViewById(R.id.username)).getText().toString(); String pass = ((EditText) findViewById(R.id.password)).getText().toString(); - + findViewById(R.id.loadBar).setVisibility(View.VISIBLE); User utente = new User(user, pass); + new LoginTask().execute(utente); + /* boolean connected = utente.connetti(); if (!connected) {//user non presente Toast.makeText(getApplicationContext(), "Credenziali non valide", Toast.LENGTH_SHORT).show(); @@ -69,7 +100,7 @@ public void onStartClick(View v) { extras.putParcelable("utentec",utente); i.putExtras(extras); startActivity(i); - } + }*/ } @@ -78,3 +109,4 @@ public void Register(View v) { startActivity(i); } } + diff --git a/app/src/main/java/com/example/raffaele/testapp/User.java b/app/src/main/java/com/example/raffaele/testapp/User.java index 8dcf61b..38e61d5 100644 --- a/app/src/main/java/com/example/raffaele/testapp/User.java +++ b/app/src/main/java/com/example/raffaele/testapp/User.java @@ -19,7 +19,7 @@ public class User implements Parcelable { //costanti della classe private final String url_login = "https://k12-api.mybluemix.net/oauth"; - private final String url_info = "https://mysql-raffysommy-1.c9.io/api/user/info"; + private final String url_info = "https://k12-api.mybluemix.net/api/user/info"; private final String url_score = "https://mysql-raffysommy-1.c9.io/api/question/score"; private final String params = "grant_type=password&client_id=student-app&client_secret=student-app-pw&"; //membri privati @@ -70,7 +70,7 @@ public boolean connetti() { //richiesta http al backend HTMLRequest dl = new HTMLRequest(url_login, params + "username="+this.username+"&password="+this.password); //richiede json di risposta - String result = dl.getHTMLThread(); + String result = dl.getHTML(); //debug + console = <3 //Log.i("User", "User.result =" + result); //estrapola dati @@ -83,15 +83,33 @@ public boolean connetti() { e.printStackTrace(); } //chiamo API per informazioni utente - downloadInfoUser(); + //downloadInfoUser(); + + //richiesta http al backend + dl = new HTMLRequest(url_info, "&access_token="+this.getAccessToken()); + //richiede json di risposta + result = dl.getHTML(); + //estrapola dati + try { + data = new JSONObject(result); + setID("id"); + setRole("role"); + setLastName(data.getString("lastName")); + setFirstName(data.getString("firstName")); + setSchool("Scuola"); + setEmail(data.getString("email")); + } catch (Exception e) { + e.printStackTrace(); + } + //prova return data != null; //Stato connessione } //retrieve info about user from online API - public void downloadInfoUser() { + /*public void downloadInfoUser() { //richiesta http al backend HTMLRequest dl = new HTMLRequest(url_info, "&access_token="+this.getAccessToken()); //richiede json di risposta - String result = dl.getHTMLThread(); + String result = dl.getHTML(); //estrapola dati JSONObject data; try { @@ -100,12 +118,12 @@ public void downloadInfoUser() { setRole("role"); setLastName(data.getString("lastName")); setFirstName(data.getString("firstName")); - setSchool("Scuola"/*data.getString("school")*/); + setSchool("Scuola"); setEmail(data.getString("email")); } catch (Exception e) { e.printStackTrace(); } - } + }*/ //permette il salvataggio online del risultato di una domanda. Ritorna il campo message JSON public String saveScore(ArrayList scores) { //da ArrayList a JSON @@ -113,8 +131,8 @@ public String saveScore(ArrayList scores) { Log.i("JSON SCORE=>", jsonA.toString()); //richiesta http al backend HTMLRequest dl = new HTMLRequest(url_score, params + - "access_token=" + this.access_token + - "&scores=" + jsonA.toString() + "access_token=" + this.access_token + + "&scores=" + jsonA.toString() ); //invoco api String result = dl.getHTMLThread(); diff --git a/app/src/main/res/layout-land/activity_main.xml b/app/src/main/res/layout-land/activity_main.xml index 8d284d5..2afbbb8 100644 --- a/app/src/main/res/layout-land/activity_main.xml +++ b/app/src/main/res/layout-land/activity_main.xml @@ -106,4 +106,18 @@ + diff --git a/app/src/main/res/layout-port/activity_main.xml b/app/src/main/res/layout-port/activity_main.xml index 8ec6ac1..c43543e 100644 --- a/app/src/main/res/layout-port/activity_main.xml +++ b/app/src/main/res/layout-port/activity_main.xml @@ -24,7 +24,10 @@ android:onClick="Register" android:background="@drawable/ic_signup" android:layout_gravity="center_horizontal" - android:layout_alignTop="@+id/loginButton" /> + android:layout_below="@+id/linearLayout4" + android:layout_alignParentLeft="true" + android:layout_alignParentStart="true" + android:layout_alignBottom="@+id/loginButton" /> + android:layout_below="@+id/linearLayout4" + android:layout_alignRight="@+id/linearLayout4" + android:layout_alignEnd="@+id/linearLayout4" /> + + + android:layout_alignParentStart="true"> + android:layout_alignParentStart="true" /> diff --git a/app/src/main/res/layout-sw600dp-land/activity_main.xml b/app/src/main/res/layout-sw600dp-land/activity_main.xml index ee8f456..4e2c79b 100644 --- a/app/src/main/res/layout-sw600dp-land/activity_main.xml +++ b/app/src/main/res/layout-sw600dp-land/activity_main.xml @@ -27,6 +27,18 @@ android:layout_alignEnd="@+id/imageView4" android:layout_alignParentBottom="true" /> + + + +