Android Open Source - NativeClient-Android Todo List Http Service






From Project

Back to project page NativeClient-Android.

License

The source code is released under:

Apache License

If you think the Android project NativeClient-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.microsoft.aad.taskapplication.helpers;
//w  w w  .j  a  v  a2s .  c  om
import android.os.AsyncTask;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class TodoListHttpService {

    private class TodoServiceGetAll extends AsyncTask<String, Void, List<String>> {

        @Override
        protected List<String> doInBackground(String... params) {
            HttpURLConnection conn = null;
            BufferedReader br = null;
            try {
                List<String> items = new ArrayList<>();
                URL url = new URL(Constants.SERVICE_URL);
                conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setRequestProperty("Content-Type", "application/json");
                conn.setRequestProperty("Authorization", "Bearer " + params[0]);
                if (conn.getResponseCode() != 200) {
                    throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
                }

                br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
                String apiOutput = br.readLine();
                JSONArray jsonArray = new JSONArray(apiOutput);
                JSONObject obj = null;
                for (int i = 0; i < jsonArray.length(); i++) {
                    obj = jsonArray.getJSONObject(i);
                    items.add(obj.getString("Title"));
                }
                return items;
            } catch (Exception e) {
                return new ArrayList<>();
            } finally {
                AppHelper.close(br);
                if (conn != null) {
                    conn.disconnect();
                }
            }
        }
    }


    private class TodoServicePostTask extends AsyncTask<String, Void, Void> {

        @Override
        protected Void doInBackground(String... params) {
            HttpURLConnection conn = null;
            BufferedReader br = null;
            try {
                List<String> items = new ArrayList<>();
                URL url = new URL(Constants.SERVICE_URL);
                conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setRequestProperty("Content-Type", "application/json");
                conn.setRequestProperty("Authorization", "Bearer " + params[1]);
                String urlParameters = "{        \"Title\": \"" + params[0] + "\"    }";

                // Send post request
                conn.setDoOutput(true);
                DataOutputStream wr = new DataOutputStream(conn.getOutputStream());
                wr.writeBytes(urlParameters);
                wr.flush();
                wr.close();

                int responseCode = conn.getResponseCode();
                if (responseCode != 200 || responseCode != 204) {
                    throw new Exception("invalid response code:" + responseCode);
                }
            } catch (Exception e) {
                //TODO - do what?
            } finally {
                AppHelper.close(br);
                if (conn != null) {
                    conn.disconnect();
                }
            }
            return null;
        }
    }

    public List<String> getAllItems(String token) throws Exception {
        return new TodoServiceGetAll().execute(token).get();
    }


    public void addItem(String title, String token) throws Exception {
        new TodoServicePostTask().execute(title, token);
    }
}




Java Source Code List

com.azure.webapi.ApiJsonOperationCallback.java
com.azure.webapi.ApiOperationCallback.java
com.azure.webapi.DateSerializer.java
com.azure.webapi.HttpPatch.java
com.azure.webapi.JsonEntityParser.java
com.azure.webapi.LoginManager.java
com.azure.webapi.LongSerializer.java
com.azure.webapi.MobileServiceAuthenticationProvider.java
com.azure.webapi.MobileServiceClient.java
com.azure.webapi.MobileServiceConnection.java
com.azure.webapi.MobileServiceException.java
com.azure.webapi.MobileServiceJsonTable.java
com.azure.webapi.MobileServiceQueryOperations.java
com.azure.webapi.MobileServiceQuery.java
com.azure.webapi.MobileServiceTableBase.java
com.azure.webapi.MobileServiceTable.java
com.azure.webapi.MobileServiceUser.java
com.azure.webapi.NextServiceFilterCallback.java
com.azure.webapi.QueryOrder.java
com.azure.webapi.RequestAsyncTask.java
com.azure.webapi.ServiceFilterRequestImpl.java
com.azure.webapi.ServiceFilterRequest.java
com.azure.webapi.ServiceFilterResponseCallback.java
com.azure.webapi.ServiceFilterResponseImpl.java
com.azure.webapi.ServiceFilterResponse.java
com.azure.webapi.ServiceFilter.java
com.azure.webapi.TableDeleteCallback.java
com.azure.webapi.TableJsonOperationCallback.java
com.azure.webapi.TableJsonQueryCallback.java
com.azure.webapi.TableOperationCallback.java
com.azure.webapi.TableQueryCallback.java
com.azure.webapi.UserAuthenticationCallback.java
com.microsoft.aad.taskapplication.AddTaskActivity.java
com.microsoft.aad.taskapplication.ApplicationTest.java
com.microsoft.aad.taskapplication.SettingsActivity.java
com.microsoft.aad.taskapplication.SimpleAlertDialog.java
com.microsoft.aad.taskapplication.ToDoActivity.java
com.microsoft.aad.taskapplication.UsersListActivity.java
com.microsoft.aad.taskapplication.helpers.AppHelper.java
com.microsoft.aad.taskapplication.helpers.Connection.java
com.microsoft.aad.taskapplication.helpers.Constants.java
com.microsoft.aad.taskapplication.helpers.InMemoryCacheStore.java
com.microsoft.aad.taskapplication.helpers.JsonHelper.java
com.microsoft.aad.taskapplication.helpers.TodoListHttpService.java
com.microsoft.aad.taskapplication.helpers.Utils.java
com.microsoft.aad.taskapplication.helpers.WorkItemAdapter.java
com.microsoft.aad.taskapplication.helpers.WorkItem.java