mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-27 07:49:28 +00:00
Improved API plus work started on friend list
This commit is contained in:
parent
f28651df53
commit
81c2e390c2
@ -3,6 +3,24 @@
|
||||
<component name="EntryPointsManager">
|
||||
<entry_points version="2.0" />
|
||||
</component>
|
||||
<component name="JavadocGenerationManager">
|
||||
<option name="OUTPUT_DIRECTORY" value="$USER_HOME$/Desktop/test" />
|
||||
<option name="OPTION_SCOPE" value="protected" />
|
||||
<option name="OPTION_HIERARCHY" value="true" />
|
||||
<option name="OPTION_NAVIGATOR" value="true" />
|
||||
<option name="OPTION_INDEX" value="true" />
|
||||
<option name="OPTION_SEPARATE_INDEX" value="true" />
|
||||
<option name="OPTION_DOCUMENT_TAG_USE" value="false" />
|
||||
<option name="OPTION_DOCUMENT_TAG_AUTHOR" value="false" />
|
||||
<option name="OPTION_DOCUMENT_TAG_VERSION" value="false" />
|
||||
<option name="OPTION_DOCUMENT_TAG_DEPRECATED" value="true" />
|
||||
<option name="OPTION_DEPRECATED_LIST" value="true" />
|
||||
<option name="OTHER_OPTIONS" />
|
||||
<option name="HEAP_SIZE" />
|
||||
<option name="LOCALE" />
|
||||
<option name="OPEN_IN_BROWSER" value="true" />
|
||||
<option name="OPTION_INCLUDE_LIBS" value="false" />
|
||||
</component>
|
||||
<component name="NullableNotNullManager">
|
||||
<option name="myDefaultNullable" value="android.support.annotation.Nullable" />
|
||||
<option name="myDefaultNotNull" value="android.support.annotation.NonNull" />
|
||||
|
@ -3,11 +3,14 @@ package org.communiquons.android.comunic.client.api;
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
import android.util.Log;
|
||||
|
||||
import org.communiquons.android.comunic.client.BuildConfig;
|
||||
import org.communiquons.android.comunic.client.data.Account.Account;
|
||||
import org.communiquons.android.comunic.client.data.Utilities;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@ -84,7 +87,7 @@ public class APIRequest {
|
||||
result.setResponse_code(conn.getResponseCode());
|
||||
|
||||
is = conn.getInputStream();
|
||||
String response = readIt(is, 5000);
|
||||
String response = readIt(is);
|
||||
result.setResponse(response);
|
||||
|
||||
conn.disconnect();
|
||||
@ -99,11 +102,18 @@ public class APIRequest {
|
||||
}
|
||||
|
||||
// Reads an InputStream and converts it to a String.
|
||||
private String readIt(InputStream stream, int len) throws IOException {
|
||||
Reader reader = new InputStreamReader(stream, "UTF-8");
|
||||
char[] buffer = new char[len];
|
||||
reader.read(buffer);
|
||||
return new String(buffer);
|
||||
private String readIt(InputStream stream) throws IOException {
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
|
||||
StringBuilder out = new StringBuilder();
|
||||
String line;
|
||||
while((line = reader.readLine()) != null){
|
||||
out.append(line);
|
||||
}
|
||||
reader.close();
|
||||
|
||||
return out.toString();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,5 +1,6 @@
|
||||
package org.communiquons.android.comunic.client.api;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
@ -81,4 +82,28 @@ public class APIResponse {
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the response as a JSON array
|
||||
*
|
||||
* Might be required in some case
|
||||
*
|
||||
* Warning ! Take care : use getJSONArray or getJSONObject in an adapted way to the response
|
||||
*
|
||||
* @return The response as JSON object. False in case of failure
|
||||
*/
|
||||
public JSONArray getJSONArray(){
|
||||
|
||||
JSONArray response = null;
|
||||
|
||||
//Try to decode JSON object
|
||||
try {
|
||||
response = new JSONArray(this.response);
|
||||
} catch (JSONException e) {
|
||||
//In case of failure
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
@ -11,9 +11,9 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Formatter;
|
||||
import java.security.MessageDigest;
|
||||
|
||||
/**
|
||||
* Application utilities
|
||||
|
@ -0,0 +1,87 @@
|
||||
package org.communiquons.android.comunic.client.data.friendsList;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import org.communiquons.android.comunic.client.api.APIRequest;
|
||||
import org.communiquons.android.comunic.client.api.APIRequestParameters;
|
||||
import org.communiquons.android.comunic.client.api.APIResponse;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* Fetch and return friends list
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
* Created by pierre on 11/12/17.
|
||||
*/
|
||||
|
||||
class GetFriendsList {
|
||||
|
||||
/**
|
||||
* The context of execution of the application
|
||||
*/
|
||||
private Context mContext;
|
||||
|
||||
/**
|
||||
* Public constructor of the class
|
||||
*
|
||||
* @param context the context of execution of the application
|
||||
*/
|
||||
GetFriendsList(Context context){
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get and return the friends list
|
||||
*
|
||||
* @return The list of friend of the user
|
||||
*/
|
||||
ArrayList<Friend> get(){
|
||||
|
||||
//Prepare the API request
|
||||
APIRequestParameters params = new APIRequestParameters(mContext, "friends/getList");
|
||||
|
||||
//Prepare the result
|
||||
ArrayList<Friend> friends = new ArrayList<>();
|
||||
|
||||
try {
|
||||
|
||||
//Perform the request and retrieve the response
|
||||
APIResponse response = new APIRequest().exec(params);
|
||||
JSONArray friendsList = response.getJSONArray();
|
||||
|
||||
if(friendsList == null)
|
||||
return null;
|
||||
|
||||
//Process JSON array
|
||||
for(int i = 0; i < friendsList.length(); i++){
|
||||
|
||||
//Try to extract JSON object containing informations
|
||||
JSONObject friendship_infos = friendsList.getJSONObject(i);
|
||||
|
||||
//Save informations about the friend in the friend object
|
||||
Friend friend = new Friend();
|
||||
|
||||
//Set friend informations
|
||||
friend.setId(friendship_infos.getInt("ID_friend"));
|
||||
friend.setAccepted(friendship_infos.getInt("accepted") == 1);
|
||||
friend.setFollowing(friendship_infos.getInt("ID_friend") == 1);
|
||||
friend.setLast_activity(friendship_infos.getInt("time_last_activity"));
|
||||
|
||||
//Add the friend to the list
|
||||
friends.add(friend);
|
||||
|
||||
}
|
||||
|
||||
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
return friends;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package org.communiquons.android.comunic.client.data.friendsList;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* This class handles the asynchronous refresh of the friends list
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
* Created by pierre on 11/12/17.
|
||||
*/
|
||||
|
||||
public abstract class GetFriendsListTask extends AsyncTask<Void, Void, ArrayList<Friend>> {
|
||||
|
||||
/**
|
||||
* The context of the task
|
||||
*/
|
||||
private Context mContext;
|
||||
|
||||
/**
|
||||
* Public constructor to the class
|
||||
*
|
||||
* @param context the context of execution of the refresh task
|
||||
*/
|
||||
public GetFriendsListTask(Context context){
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
/**
|
||||
* What to to once the users list has been refreshed
|
||||
*
|
||||
* It it the role of each class that use this task class to implement this method
|
||||
*
|
||||
* @param friendsList The list of friends of the user / null in case of failure
|
||||
*/
|
||||
@Override
|
||||
abstract protected void onPostExecute(ArrayList<Friend> friendsList);
|
||||
|
||||
/**
|
||||
* Background operation
|
||||
*
|
||||
* @param params
|
||||
* @return The list of friends of the user
|
||||
*/
|
||||
@Override
|
||||
protected ArrayList<Friend> doInBackground(Void... params) {
|
||||
//Create the GetFriendList object and use it to fetch the list
|
||||
return new GetFriendsList(mContext).get();
|
||||
}
|
||||
}
|
@ -14,6 +14,8 @@ import org.communiquons.android.comunic.client.R;
|
||||
import org.communiquons.android.comunic.client.data.DatabaseHelper;
|
||||
import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersInfos;
|
||||
import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo;
|
||||
import org.communiquons.android.comunic.client.data.friendsList.Friend;
|
||||
import org.communiquons.android.comunic.client.data.friendsList.GetFriendsListTask;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@ -43,27 +45,29 @@ public class FriendsListFragment extends Fragment {
|
||||
public void onStart() {
|
||||
super.onStart();
|
||||
|
||||
final ArrayList<Integer> list = new ArrayList<>();
|
||||
list.add(2);
|
||||
list.add(3);
|
||||
new GetUsersInfos(getActivity(), new DatabaseHelper(getActivity())).getMultiple(list, new GetUsersInfos.getMultipleUserInfosCallback() {
|
||||
//Refresh the friends list
|
||||
refresh_friend_list();
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the friend list
|
||||
*/
|
||||
void refresh_friend_list(){
|
||||
new GetFriendsListTask(getActivity().getApplicationContext()){
|
||||
|
||||
@Override
|
||||
public void callback(ArrayMap<Integer, UserInfo> info) {
|
||||
Log.v("FriendsListFragment", "User infos callback");
|
||||
if(info == null)
|
||||
Toast.makeText(getActivity(), "Failure", Toast.LENGTH_LONG).show();
|
||||
else
|
||||
Toast.makeText(getActivity(), "Success", Toast.LENGTH_LONG).show();
|
||||
protected void onPostExecute(ArrayList<Friend> friendsList) {
|
||||
|
||||
for(int ID : list){
|
||||
UserInfo infos = info.get(ID);
|
||||
//Display informations in the console
|
||||
for(Friend friend : friendsList){
|
||||
Log.v("FriendsListFragment", "Friend: " + friend.getId() + " " +
|
||||
(friend.isAccepted() ? 1 : 0 ) + " " + (friend.isFollowing() ? 1 : 0) +
|
||||
" " + friend.getLast_activity() + " "
|
||||
);
|
||||
}
|
||||
|
||||
if(infos == null)
|
||||
Log.e("FriendsListFragment", "Error with user infos for ID " + ID);
|
||||
else
|
||||
Log.v("FriendsListFragment", ID + " is " + infos.getFullName() + " !");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
}.execute();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user