Search in sources :

Example 96 with JsonObjectRequest

use of com.android.volley.toolbox.JsonObjectRequest in project yellowmessenger-sdk by yellowmessenger.

the class NotificationUtil method sendDeviceTokenToServer.

public static void sendDeviceTokenToServer(String deviceId, String username, final String authorizationToken, Context context) {
    RequestQueue queue = Volley.newRequestQueue(context);
    String url = "https://notifications.botplatform.io/push/registerDevice";
    deviceId = FirebaseInstanceId.getInstance().getToken();
    JSONObject body = new JSONObject();
    try {
        body.put("deviceId", deviceId);
        body.put("profileId", username);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, body, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    if (response.getBoolean("success")) {
                    } else {
                    }
                } catch (Exception e) {
                }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
            }
        }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<>();
                headers.put("bp-auth-token", authorizationToken);
                return headers;
            }
        };
        queue.add(jsonObjectRequest);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Response(com.android.volley.Response) VolleyError(com.android.volley.VolleyError) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) RequestQueue(com.android.volley.RequestQueue) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Example 97 with JsonObjectRequest

use of com.android.volley.toolbox.JsonObjectRequest in project IceNet by anton46.

the class NetworkManager method fromJsonObject.

private void fromJsonObject(final HashMap<String, String> headers, HashMap<String, Object> bodyRequest, String requestTag, final RequestCallback requestCallback) {
    JsonObjectRequest request = new JsonObjectRequest(method, getUrlConnection(pathUrl), createBodyRequest(bodyRequest), new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject jsonObject) {
            Object t = new Gson().fromJson(jsonObject.toString(), classTarget.getType());
            if (requestCallback != null)
                requestCallback.onRequestSuccess(t);
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            if (requestCallback != null) {
                NetworkResponse response = error.networkResponse;
                if (response != null)
                    requestCallback.onRequestError(new RequestError(response));
            }
        }
    }) {

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            return headers != null ? headers : super.getHeaders();
        }
    };
    networkHelper.addToRequestQueue(request, requestTag);
}
Also used : Response(com.android.volley.Response) NetworkResponse(com.android.volley.NetworkResponse) VolleyError(com.android.volley.VolleyError) JSONObject(org.json.JSONObject) NetworkResponse(com.android.volley.NetworkResponse) Gson(com.google.gson.Gson) JSONObject(org.json.JSONObject) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Example 98 with JsonObjectRequest

use of com.android.volley.toolbox.JsonObjectRequest in project FastDev4Android by jiangqqlmj.

the class JsonRequestCharsetTest method specifiedCharsetJsonObject.

@Test
public void specifiedCharsetJsonObject() throws Exception {
    byte[] data = jsonObjectString().getBytes(Charset.forName("ISO-8859-1"));
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json; charset=iso-8859-1");
    NetworkResponse network = new NetworkResponse(data, headers);
    JsonObjectRequest objectRequest = new JsonObjectRequest("", null, null, null);
    Response<JSONObject> objectResponse = objectRequest.parseNetworkResponse(network);
    assertNotNull(objectResponse);
    assertTrue(objectResponse.isSuccess());
    // don't check the text in Czech, ISO-8859-1 doesn't support some Czech characters
    assertEquals(COPY_VALUE, objectResponse.result.getString(COPY_NAME));
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) NetworkResponse(com.android.volley.NetworkResponse) String(java.lang.String) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest) Test(org.junit.Test)

Example 99 with JsonObjectRequest

use of com.android.volley.toolbox.JsonObjectRequest in project FastDev4Android by jiangqqlmj.

the class JsonRequestCharsetTest method defaultCharsetJsonObject.

@Test
public void defaultCharsetJsonObject() throws Exception {
    // UTF-8 is default charset for JSON
    byte[] data = jsonObjectString().getBytes(Charset.forName("UTF-8"));
    NetworkResponse network = new NetworkResponse(data);
    JsonObjectRequest objectRequest = new JsonObjectRequest("", null, null, null);
    Response<JSONObject> objectResponse = objectRequest.parseNetworkResponse(network);
    assertNotNull(objectResponse);
    assertTrue(objectResponse.isSuccess());
    assertEquals(TEXT_VALUE, objectResponse.result.getString(TEXT_NAME));
    assertEquals(COPY_VALUE, objectResponse.result.getString(COPY_NAME));
}
Also used : JSONObject(org.json.JSONObject) NetworkResponse(com.android.volley.NetworkResponse) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest) Test(org.junit.Test)

Example 100 with JsonObjectRequest

use of com.android.volley.toolbox.JsonObjectRequest in project MPW by shineangelic.

the class NoobPoolInstrumentedTest method testJsonMinerRequest.

@Test
public void testJsonMinerRequest() throws Exception {
    final GsonBuilder builder = new GsonBuilder();
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, Utils.getMinersStatsUrl(sharedPreferences), null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(final JSONObject response) {
            Log.i(Constants.TAG, response.toString());
            Gson gson = builder.create();
            // Register an adapter to manage the date types as long values
            MinerRoot retrieved = gson.fromJson(response.toString(), MinerRoot.class);
            minerAddr = retrieved.getMiners().values().iterator().next().getAddress();
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(Constants.TAG, "Error: " + error.getMessage());
        }
    });
    // Adding request to request queue
    JSONClientSingleton.getInstance(appContext).addToRequestQueue(jsonObjReq);
}
Also used : Response(com.android.volley.Response) VolleyError(com.android.volley.VolleyError) JSONObject(org.json.JSONObject) MinerRoot(it.angelic.mpw.model.jsonpojos.miners.MinerRoot) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest) Test(org.junit.Test)

Aggregations

JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)129 JSONObject (org.json.JSONObject)124 VolleyError (com.android.volley.VolleyError)102 Response (com.android.volley.Response)101 RequestQueue (com.android.volley.RequestQueue)70 HashMap (java.util.HashMap)66 JSONException (org.json.JSONException)59 User (model.User)50 Gson (com.google.gson.Gson)30 Test (org.junit.Test)22 Context (android.content.Context)19 Toast (android.widget.Toast)17 GsonBuilder (com.google.gson.GsonBuilder)17 NetworkResponse (com.android.volley.NetworkResponse)16 DefaultRetryPolicy (com.android.volley.DefaultRetryPolicy)12 TextView (android.widget.TextView)11 Wallet (it.angelic.mpw.model.jsonpojos.wallet.Wallet)9 ExecutionException (java.util.concurrent.ExecutionException)8 HomeStats (it.angelic.mpw.model.jsonpojos.home.HomeStats)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7