Android Open Source - RoommateManager Json Object Request Handler






From Project

Back to project page RoommateManager.

License

The source code is released under:

MIT License

If you think the Android project RoommateManager 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 edu.iupui.gdvander.roommatemanager.handler;
//from   w ww.j  a  va  2  s  . c  om
/**
 * Created by Gerrit VanderLugt
 * Title: JsonObjectRequestHandler.java
 * Purpose: Handle http requests using Volley's JSONObjectRequest.
 */

import org.json.JSONObject;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;

import java.util.HashMap;
import java.util.Map;

import edu.iupui.gdvander.roommatemanager.app.VolleySingleton;

public class JsonObjectRequestHandler {

    //Declare the base url to be used. Should be where the API is hosted.  In this case, localhost.
    private String baseURL = "http://192.168.0.10:9080";

    //Initialize the url variable. This will be added to the base URL and is specified by the
    //class implementing the request.
    private String url;

    public void post(String path,
                     JSONObject jsonRequest,
                     Response.Listener<JSONObject> listener,
                     Response.ErrorListener errorListener){

        //Construct the url
        url = baseURL + path;

        //Send Volley json POST request including the given json object and listeners
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                Request.Method.POST, url, jsonRequest, listener, errorListener){
            @Override
            public Map<String,String> getHeaders() throws AuthFailureError {
                Map<String,String> params = new HashMap<String,String>();
                params.put("Content-Type","application/x-www-form-urlencoded");
                return params;
            }
        };

        //Access the RequestQueue through the singleton class
        VolleySingleton.getInstance().addToRequestQueue(jsonObjectRequest);

    }

    public void get(String path,
                    Response.Listener<JSONObject> listener,
                    Response.ErrorListener errorListener){
        //Construct the url
        url = (baseURL + path);

        //Send Volley json GET request including the given json object and listeners
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                Request.Method.GET, url, null, listener, errorListener){
        };

        //Access the RequestQueue through the singleton class
        VolleySingleton.getInstance().addToRequestQueue(jsonObjectRequest);
    }

}




Java Source Code List

edu.iupui.gdvander.roommatemanager.app.LoginActivity.java
edu.iupui.gdvander.roommatemanager.app.MainActivity.java
edu.iupui.gdvander.roommatemanager.app.NavigationDrawerFragment.java
edu.iupui.gdvander.roommatemanager.app.RegisterActivity.java
edu.iupui.gdvander.roommatemanager.app.SplashActivity.java
edu.iupui.gdvander.roommatemanager.app.VolleySingleton.java
edu.iupui.gdvander.roommatemanager.app.finances.FinancesFragment.java
edu.iupui.gdvander.roommatemanager.app.finances.FinancesPagerAdapter.java
edu.iupui.gdvander.roommatemanager.app.finances.tabs.DebtsTab.java
edu.iupui.gdvander.roommatemanager.app.finances.tabs.ExpensesTab.java
edu.iupui.gdvander.roommatemanager.app.finances.tabs.PersonalTransactionTab.java
edu.iupui.gdvander.roommatemanager.app.finances.tabs.SharedTransactionTab.java
edu.iupui.gdvander.roommatemanager.app.finances.tabs.TransferTab.java
edu.iupui.gdvander.roommatemanager.app.groceries.GroceriesFragment.java
edu.iupui.gdvander.roommatemanager.app.groceries.GroceryArrayAdapter.java
edu.iupui.gdvander.roommatemanager.app.groceries.GroceryItemDialog.java
edu.iupui.gdvander.roommatemanager.app.groceries.GroceryItem.java
edu.iupui.gdvander.roommatemanager.app.home.HomeFragment.java
edu.iupui.gdvander.roommatemanager.app.home.HomePagerAdapter.java
edu.iupui.gdvander.roommatemanager.app.home.tabs.DetailsTab.java
edu.iupui.gdvander.roommatemanager.app.home.tabs.HomeTab.java
edu.iupui.gdvander.roommatemanager.app.todo.ToDoFragment.java
edu.iupui.gdvander.roommatemanager.app.todo.ToDoPagerAdapter.java
edu.iupui.gdvander.roommatemanager.app.todo.tabs.MyToDoTab.java
edu.iupui.gdvander.roommatemanager.app.todo.tabs.SharedToDoTab.java
edu.iupui.gdvander.roommatemanager.handler.JsonObjectRequestHandler.java
edu.iupui.gdvander.roommatemanager.handler.KeyboardHandler.java