Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class TestUrlConection {
- private static String responseServer = "";
- private static String method = null;
- private static ProgressDialog progressDialog = null;
- private static String getFireBase = null;
- private static String urlAddress;
- private static Context activityContext;
- public static OnAsyncRequestComplete caller;
- public interface OnAsyncRequestComplete {
- void asyncResponse(String response);
- }// Fin OnAsyncRequestComplete
- public static void request(Context activity, String url, String requestMethod ){
- activityContext = activity;
- urlAddress = url;
- method = requestMethod;
- caller = ( OnAsyncRequestComplete )activityContext;
- new MakeRequest().execute();
- }// Fin request
- public static class MakeRequest extends AsyncTask<String, String, String>{
- @Override
- protected void onPreExecute() {
- super.onPreExecute();
- progressDialog = new ProgressDialog(activityContext);
- }
- @Override
- protected void onPostExecute(String response) {
- super.onPostExecute(response);
- if (progressDialog != null && progressDialog.isShowing()) {
- progressDialog.dismiss();
- }
- caller.asyncResponse( response );
- }// Fin onPostExecute
- @Override
- protected void onProgressUpdate(String... values) {
- super.onProgressUpdate( values );
- }
- @Override
- protected void onCancelled(String s) {
- super.onCancelled(s);
- }
- @Override
- protected void onCancelled() {
- super.onCancelled();
- }
- @Override
- protected String doInBackground(String... strings) {
- if (method == "POST") {
- }
- if (method == "GET") {
- try {
- responseServer = get();
- } catch (InterruptedException e) {
- e.printStackTrace();
- } catch (ExecutionException e) {
- e.printStackTrace();
- }
- }
- if (method == "GETTOKEN") {
- return firebaseinstanceIdTask();
- }
- return null;
- }// Fin doInBackground
- }// Fin MakeRequest
- private String get() {
- try {
- URL urlServer = new URL( urlAddress );
- HttpURLConnection conectionServer = (HttpURLConnection) urlServer.openConnection();
- conectionServer.setRequestMethod(method);
- conectionServer.setRequestProperty("content-type", "application/json");
- conectionServer.connect();
- InputStream inputStream = conectionServer.getInputStream();
- StringBuffer stringBuffer = new StringBuffer();
- if (inputStream == null) {
- return null;
- }
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
- String line;
- while ((line = bufferedReader.readLine()) != null) {
- stringBuffer.append(line + "n");
- }
- if (stringBuffer.length() == 0) {
- return null;
- }
- String forecastJsonStr = stringBuffer.toString();
- return forecastJsonStr;
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }// fin get
- private static String firebaseinstanceIdTask() {
- FireBaseIdService instanceIDService = new FireBaseIdService();
- instanceIDService.onTokenRefresh();
- getFireBase = instanceIDService.getFireBaseToken();
- return getFireBase;
- }// Fin firebaseinstanceIdTask
- caller = ( OnAsyncRequestComplete )activityContext;
Add Comment
Please, Sign In to add comment