Can retrieve user informations on the server

This commit is contained in:
Pierre 2017-11-05 17:52:10 +01:00
parent 6089b20b9a
commit fe945c32e6
3 changed files with 61 additions and 6 deletions

View File

@ -44,15 +44,16 @@ public class MainActivity extends AppCompatActivity {
//DEVELOPMENT : Try to get information about a user over the network //DEVELOPMENT : Try to get information about a user over the network
GetUsersInfos uInfos = new GetUsersInfos(this, null); GetUsersInfos uInfos = new GetUsersInfos(this, null);
//Get infos... about me! //Get infos... about me! :)
int uID = 1; final int uID = 1;
uInfos.get(uID, new GetUsersInfos.getUserInfosCallback() { uInfos.get(uID, new GetUsersInfos.getUserInfosCallback() {
@Override @Override
public void callback(UserInfo info) { public void callback(UserInfo info) {
if(info == null) if(info == null)
Toast.makeText(MainActivity.this, "Failure !", Toast.LENGTH_SHORT).show(); Toast.makeText(MainActivity.this, "Failure !", Toast.LENGTH_SHORT).show();
else else {
Toast.makeText(MainActivity.this, "Success !", Toast.LENGTH_SHORT).show(); Toast.makeText(MainActivity.this, uID + " is " + info.getFullName() + "!", Toast.LENGTH_SHORT).show();
}
} }
}); });
} }

View File

@ -148,6 +148,8 @@ public abstract class APIRequestTask extends AsyncTask<APIRequestParameters, Voi
/** /**
* Add the login tokens to an API request object * Add the login tokens to an API request object
*
* @param params The parameters of the request to update
*/ */
private void addLoginTokens(APIRequestParameters params){ private void addLoginTokens(APIRequestParameters params){

View File

@ -6,6 +6,8 @@ import org.communiquons.android.comunic.client.api.APIRequestParameters;
import org.communiquons.android.comunic.client.api.APIRequestTask; import org.communiquons.android.comunic.client.api.APIRequestTask;
import org.communiquons.android.comunic.client.api.APIResponse; import org.communiquons.android.comunic.client.api.APIResponse;
import org.communiquons.android.comunic.client.data.DatabaseHelper; import org.communiquons.android.comunic.client.data.DatabaseHelper;
import org.json.JSONException;
import org.json.JSONObject;
/** /**
* This class handles informations requests about user informations * This class handles informations requests about user informations
@ -61,10 +63,9 @@ public class GetUsersInfos {
* *
* @param id The ID of the user to get informations from * @param id The ID of the user to get informations from
*/ */
public void get(int id, getUserInfosCallback callback){ public void get(final int id, final getUserInfosCallback callback){
//Perform a request on the API server //Perform a request on the API server
//Setup the request //Setup the request
APIRequestParameters requestParameters = new APIRequestParameters(context, "user/getInfos"); APIRequestParameters requestParameters = new APIRequestParameters(context, "user/getInfos");
requestParameters.addParameter("userID", ""+id); requestParameters.addParameter("userID", ""+id);
@ -75,7 +76,25 @@ public class GetUsersInfos {
@Override @Override
protected void onPostExecute(APIResponse result) { protected void onPostExecute(APIResponse result) {
UserInfo userInfos = null;
try {
//Try to extract user informations
JSONObject userObject = result.getJSONObject().getJSONObject(""+id);
//Continue only if we could extract required informations
if (userObject != null) {
//Parse user informations
userInfos = parse_user_json(userObject);
}
} catch (JSONException e){
e.printStackTrace();
}
//Go to the next function
callback.callback(userInfos);
} }
@ -83,4 +102,37 @@ public class GetUsersInfos {
} }
/**
* Parse a JSON object into a user object
*
* @param userObject Informations about the user in an object
* @return User object in case of success, null else
*/
private UserInfo parse_user_json(JSONObject userObject){
//Check if the JSON object passed is null or not
if(userObject == null)
return null; //Failure
UserInfo userInfos = new UserInfo();
//Try to retrieve user informations
try {
//Retrieve all user informations
userInfos.setId(userObject.getInt("userID"));
userInfos.setFirstName(userObject.getString("firstName"));
userInfos.setLastName(userObject.getString("lastName"));
userInfos.setAccountImageURL(userObject.getString("accountImage"));
} catch (JSONException e){
e.printStackTrace();
return null;
}
//Return result
return userInfos;
}
} }