Skip to content

Commit

Permalink
Passaggio a Bluemix per le API di info utente.Aggiunta LoadingBar dur…
Browse files Browse the repository at this point in the history
…ante il login. Procedura di Login resa asincrona per non caricare il thread principale
  • Loading branch information
raffysommy committed Apr 8, 2015
1 parent f62ed5a commit f73b926
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 20 deletions.
36 changes: 34 additions & 2 deletions app/src/main/java/com/example/raffaele/testapp/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,6 +15,33 @@
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
//subclass for login execution
private class LoginTask extends AsyncTask<User, Integer, User> {
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);
Expand Down Expand Up @@ -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();
Expand All @@ -69,7 +100,7 @@ public void onStartClick(View v) {
extras.putParcelable("utentec",utente);
i.putExtras(extras);
startActivity(i);
}
}*/

}

Expand All @@ -78,3 +109,4 @@ public void Register(View v) {
startActivity(i);
}
}

36 changes: 27 additions & 9 deletions app/src/main/java/com/example/raffaele/testapp/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -100,21 +118,21 @@ 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<String[]> scores) {
//da ArrayList a JSON
JSONArray jsonA = new JSONArray(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();
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/layout-land/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,18 @@

</LinearLayout>
</ScrollView>
<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="75dp"
android:layout_height="75dp"
android:id="@+id/loadBar"
android:visibility="invisible"
android:progress="50"
android:max="100"
android:indeterminate="false"
android:layout_below="@+id/scrollView"
android:layout_alignRight="@+id/imageView5"
android:layout_alignEnd="@+id/imageView5"
android:layout_alignLeft="@+id/imageView5"
android:layout_alignStart="@+id/imageView5" />
</RelativeLayout>
36 changes: 27 additions & 9 deletions app/src/main/res/layout-port/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />

<ImageButton
android:layout_width="100dip"
Expand All @@ -33,9 +36,9 @@
android:onClick="onStartClick"
android:background="@drawable/ic_login"
android:layout_gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
android:layout_below="@+id/linearLayout4"
android:layout_alignRight="@+id/linearLayout4"
android:layout_alignEnd="@+id/linearLayout4" />

<ImageView
android:layout_width="200dp"
Expand All @@ -45,15 +48,31 @@
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="75dp"
android:layout_height="75dp"
android:id="@+id/loadBar"
android:visibility="invisible"
android:progress="50"
android:max="100"
android:indeterminate="false"
android:layout_below="@+id/linearLayout4"
android:layout_centerHorizontal="true" />

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout4"
android:layout_above="@+id/RegisterButton"
android:baselineAligned="false"
android:measureWithLargestChild="false"
android:longClickable="false"
android:nestedScrollingEnabled="false"
android:clickable="false"
android:layout_below="@+id/imageView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/imageView2">
android:layout_alignParentStart="true">

<TextView
android:layout_width="wrap_content"
Expand Down Expand Up @@ -92,8 +111,7 @@
android:text="rootroot"
android:layout_above="@+id/loginButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="91dp" />
android:layout_alignParentStart="true" />

</LinearLayout>

Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/layout-sw600dp-land/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@
android:layout_alignEnd="@+id/imageView4"
android:layout_alignParentBottom="true" />

<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="75dp"
android:layout_height="75dp"
android:id="@+id/loadBar"
android:visibility="invisible"
android:progress="50"
android:max="100"
android:indeterminate="false"
android:layout_alignTop="@+id/loginButton"
android:layout_centerHorizontal="true" />

<ImageView
android:layout_width="500dp"
android:layout_height="250dp"
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/res/layout-sw600dp-port/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="75dp"
android:layout_height="75dp"
android:id="@+id/loadBar"
android:visibility="invisible"
android:progress="50"
android:max="100"
android:indeterminate="false"
android:layout_below="@+id/linearLayout"
android:layout_toRightOf="@+id/RegisterButton"
android:layout_toLeftOf="@+id/loginButton"
android:layout_toStartOf="@+id/loginButton" />

<LinearLayout
android:orientation="vertical"
android:layout_width="900dp"
Expand Down

0 comments on commit f73b926

Please sign in to comment.