mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-27 07:49:28 +00:00
Created APIRequest class (divided APIRequestTask in two separate files)
This commit is contained in:
parent
f1a6ff8502
commit
6be4603435
@ -50,7 +50,7 @@ public class LoginActivity extends AppCompatActivity {
|
||||
aUtils = new AccountUtils(this);
|
||||
|
||||
//Check for connectivity
|
||||
if(!APIRequestTask.isAPIavailable(this)){
|
||||
if(!APIRequest.isAPIavailable(this)){
|
||||
Toast.makeText(this, R.string.err_no_internet_connection, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
@ -93,7 +93,7 @@ public class LoginActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
//Check internet connection
|
||||
if(!APIRequestTask.isAPIavailable(this)){
|
||||
if(!APIRequest.isAPIavailable(this)){
|
||||
show_form_error(getString(R.string.err_no_internet_connection));
|
||||
stop = 1;
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.widget.Toast;
|
||||
|
||||
import org.communiquons.android.comunic.client.api.APIRequest;
|
||||
import org.communiquons.android.comunic.client.api.APIRequestTask;
|
||||
import org.communiquons.android.comunic.client.data.Account.Account;
|
||||
import org.communiquons.android.comunic.client.data.Account.AccountUtils;
|
||||
@ -40,15 +41,6 @@ public class MainActivity extends AppCompatActivity {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
//Enable bottom navigation menu
|
||||
init_bottom_menu();
|
||||
|
||||
//Check for connectivity
|
||||
if(!APIRequestTask.isAPIavailable(this)){
|
||||
Toast.makeText(this, R.string.err_no_internet_connection, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
//Initialize account objects
|
||||
account = new Account(this);
|
||||
@ -57,6 +49,18 @@ public class MainActivity extends AppCompatActivity {
|
||||
if(!account.signed_in()){
|
||||
//Open the login activity
|
||||
startActivity(new Intent(this, LoginActivity.class));
|
||||
return;
|
||||
}
|
||||
|
||||
//Set the content of the activity
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
//Enable bottom navigation menu
|
||||
init_bottom_menu();
|
||||
|
||||
//Check for connectivity
|
||||
if(!APIRequest.isAPIavailable(this)){
|
||||
Toast.makeText(this, R.string.err_no_internet_connection, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
//If it is the first time the application is launched, started the user friends tab
|
||||
|
@ -0,0 +1,149 @@
|
||||
package org.communiquons.android.comunic.client.api;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
|
||||
import org.communiquons.android.comunic.client.BuildConfig;
|
||||
import org.communiquons.android.comunic.client.data.Account.Account;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.Reader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* API request file
|
||||
*
|
||||
* Perform an API request
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
* Created by pierre on 11/12/17.
|
||||
*/
|
||||
|
||||
public class APIRequest {
|
||||
|
||||
/**
|
||||
* Public constructor
|
||||
*/
|
||||
public APIRequest(){
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform an API request on the server and return the result as a JSON code
|
||||
*
|
||||
* @param parameters The parameters to pass to the server
|
||||
* @return The result of the request
|
||||
*/
|
||||
public APIResponse exec(APIRequestParameters parameters) throws Exception{
|
||||
|
||||
//Add login tokens
|
||||
addLoginTokens(parameters);
|
||||
|
||||
APIResponse result = new APIResponse();
|
||||
|
||||
InputStream is = null;
|
||||
|
||||
try {
|
||||
|
||||
//Determine the URL of the request
|
||||
URL url = new URL(BuildConfig.api_url + parameters.getRequest_uri());
|
||||
|
||||
//The request is being performed on an http server
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
|
||||
//Configure the connection
|
||||
conn.setReadTimeout(3000);
|
||||
conn.setConnectTimeout(3000);
|
||||
conn.setRequestMethod("POST");
|
||||
conn.setDoInput(true);
|
||||
conn.setDoOutput(true);
|
||||
|
||||
|
||||
|
||||
//Send request parameters
|
||||
OutputStream out = new BufferedOutputStream(conn.getOutputStream());
|
||||
BufferedWriter writer = new BufferedWriter (new OutputStreamWriter(out, "UTF-8"));
|
||||
writer.write(parameters.get_parameters_encoded());
|
||||
writer.flush();
|
||||
writer.close();
|
||||
out.close();
|
||||
|
||||
//Connect to the server
|
||||
conn.connect();
|
||||
|
||||
//Get response code
|
||||
result.setResponse_code(conn.getResponseCode());
|
||||
|
||||
is = conn.getInputStream();
|
||||
String response = readIt(is, 5000);
|
||||
result.setResponse(response);
|
||||
|
||||
conn.disconnect();
|
||||
|
||||
} finally {
|
||||
//Close streams
|
||||
if(is != null)
|
||||
is.close();
|
||||
}
|
||||
//Return result
|
||||
return result;
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the login tokens to an API request object
|
||||
*
|
||||
* @param params The parameters of the request to update
|
||||
*/
|
||||
private void addLoginTokens(APIRequestParameters params){
|
||||
|
||||
//Create account object
|
||||
Account account = new Account(params.getContext());
|
||||
|
||||
//Check if user is signed in or not
|
||||
if(!account.signed_in())
|
||||
return; //Do nothing
|
||||
|
||||
//Get login tokens
|
||||
ArrayList<String> tokens = account.getLoginTokens();
|
||||
|
||||
if(tokens.size() < 2)
|
||||
return; //Not enough tokens
|
||||
|
||||
//Add them to the request
|
||||
params.addParameter("userToken1", tokens.get(0));
|
||||
params.addParameter("userToken2", tokens.get(1));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine it is possible or not to connect to the API now
|
||||
*
|
||||
* @param context The context used to perform the test
|
||||
* @return True if a network connection is available
|
||||
*/
|
||||
public static boolean isAPIavailable(Context context){
|
||||
ConnectivityManager connMgr = (ConnectivityManager)
|
||||
context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
|
||||
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
|
||||
|
||||
return networkInfo != null && networkInfo.isConnected();
|
||||
}
|
||||
}
|
@ -46,8 +46,8 @@ public abstract class APIRequestTask extends AsyncTask<APIRequestParameters, Voi
|
||||
|
||||
try {
|
||||
//Perform the API request
|
||||
addLoginTokens(params[0]);
|
||||
return downloadUrl(params[0]);
|
||||
APIRequest req = new APIRequest();
|
||||
return req.exec(params[0]);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
@ -63,111 +63,4 @@ public abstract class APIRequestTask extends AsyncTask<APIRequestParameters, Voi
|
||||
* @param result The result of the request
|
||||
*/
|
||||
abstract protected void onPostExecute(APIResponse result);
|
||||
|
||||
/**
|
||||
* Peform an API request on the server and return the result as a JSON code
|
||||
*
|
||||
* @param parameters The parameters to pass to the server
|
||||
* @return The result of the request
|
||||
*/
|
||||
private APIResponse downloadUrl(APIRequestParameters parameters) throws Exception{
|
||||
|
||||
APIResponse result = new APIResponse();
|
||||
|
||||
InputStream is = null;
|
||||
|
||||
try {
|
||||
|
||||
//Determine the URL of the request
|
||||
URL url = new URL(BuildConfig.api_url + parameters.getRequest_uri());
|
||||
|
||||
//The request is being performed on an http server
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
|
||||
//Configure the connection
|
||||
conn.setReadTimeout(3000);
|
||||
conn.setConnectTimeout(3000);
|
||||
conn.setRequestMethod("POST");
|
||||
conn.setDoInput(true);
|
||||
conn.setDoOutput(true);
|
||||
|
||||
|
||||
|
||||
//Send request parameters
|
||||
OutputStream out = new BufferedOutputStream(conn.getOutputStream());
|
||||
BufferedWriter writer = new BufferedWriter (new OutputStreamWriter(out, "UTF-8"));
|
||||
writer.write(parameters.get_parameters_encoded());
|
||||
writer.flush();
|
||||
writer.close();
|
||||
out.close();
|
||||
|
||||
//Connect to the server
|
||||
conn.connect();
|
||||
|
||||
//Get response code
|
||||
result.setResponse_code(conn.getResponseCode());
|
||||
|
||||
is = conn.getInputStream();
|
||||
String response = readIt(is, 5000);
|
||||
result.setResponse(response);
|
||||
|
||||
conn.disconnect();
|
||||
|
||||
} finally {
|
||||
//Close streams
|
||||
if(is != null)
|
||||
is.close();
|
||||
}
|
||||
//Return result
|
||||
return result;
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine it is possible or not to connect to the API now
|
||||
*
|
||||
* @param context The context used to perform the test
|
||||
* @return True if a network connection is available
|
||||
*/
|
||||
public static boolean isAPIavailable(Context context){
|
||||
ConnectivityManager connMgr = (ConnectivityManager)
|
||||
context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
|
||||
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
|
||||
|
||||
return networkInfo != null && networkInfo.isConnected();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the login tokens to an API request object
|
||||
*
|
||||
* @param params The parameters of the request to update
|
||||
*/
|
||||
private void addLoginTokens(APIRequestParameters params){
|
||||
|
||||
//Create account object
|
||||
Account account = new Account(params.getContext());
|
||||
|
||||
//Check if user is signed in or not
|
||||
if(!account.signed_in())
|
||||
return; //Do nothing
|
||||
|
||||
//Get login tokens
|
||||
ArrayList<String> tokens = account.getLoginTokens();
|
||||
|
||||
if(tokens.size() < 2)
|
||||
return; //Not enough tokens
|
||||
|
||||
//Add them to the request
|
||||
params.addParameter("userToken1", tokens.get(0));
|
||||
params.addParameter("userToken2", tokens.get(1));
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user