Search in sources :

Example 66 with JsonObjectRequest

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

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 67 with JsonObjectRequest

use of com.android.volley.toolbox.JsonObjectRequest in project physical-web by google.

the class UrlShortenerClient method shortenUrl.

public void shortenUrl(final String longUrl, final ShortenUrlCallback shortenUrlCallback, final String tag) {
    // Create the json payload
    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("longUrl", longUrl);
    } catch (JSONException e) {
        Log.e(TAG, "JSONException: " + e.toString());
        shortenUrlCallback.onError(longUrl);
        return;
    }
    // Create the http request
    JsonObjectRequest request = new JsonObjectRequest(constructUrlStr(SHORTEN_URL_PATH), jsonObject, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject jsonResponse) {
            String shortUrl;
            try {
                shortUrl = jsonResponse.getString("id");
            } catch (JSONException e) {
                Log.e(TAG, "JSONException: " + e.toString());
                shortenUrlCallback.onError(longUrl);
                return;
            }
            shortenUrlCallback.onUrlShortened(shortUrl);
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Log.i(TAG, "VolleyError: " + volleyError.toString());
            shortenUrlCallback.onError(longUrl);
        }
    });
    request.setTag(tag);
    // Send off the request
    mRequestQueue.add(request);
}
Also used : Response(com.android.volley.Response) VolleyError(com.android.volley.VolleyError) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Example 68 with JsonObjectRequest

use of com.android.volley.toolbox.JsonObjectRequest in project TaEmCasa by Dionen.

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 69 with JsonObjectRequest

use of com.android.volley.toolbox.JsonObjectRequest in project Space-Station-Tracker by Kiarasht.

the class Locations method displayResults.

/**
     * After successfully getting a Latitude and Longitude from the API, search a database to see
     * what city and country do these correspond to.
     */
private void displayResults() {
    // Returns a JSONObject
    final String url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + mLatitude + "," + mLongitude + "&sensor=false";
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            try {
                // Save the formatted address, we will use it later
                JSONObject results = response.getJSONArray("results").getJSONObject(1);
                mLocation = results.getString("formatted_address");
                SightSee.setLocation(mLocation);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError e) {
            Toast.makeText(Locations.this, R.string.errorNetwork, Toast.LENGTH_LONG).show();
        }
    });
    jsonObjectRequest.setTag(TAG);
    requestQueue.add(jsonObjectRequest);
}
Also used : Response(com.android.volley.Response) VolleyError(com.android.volley.VolleyError) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) JsonObjectRequest(com.android.volley.toolbox.JsonObjectRequest)

Example 70 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)

Aggregations

JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)70 JSONObject (org.json.JSONObject)70 Response (com.android.volley.Response)62 VolleyError (com.android.volley.VolleyError)62 RequestQueue (com.android.volley.RequestQueue)54 HashMap (java.util.HashMap)54 User (model.User)50 JSONException (org.json.JSONException)42 Context (android.content.Context)17 Toast (android.widget.Toast)17 TextView (android.widget.TextView)8 NetworkResponse (com.android.volley.NetworkResponse)8 Test (org.junit.Test)6 Intent (android.content.Intent)4 Switch (android.widget.Switch)4 String (java.lang.String)3 Aplicativo (model.Aplicativo)3 Arduino (model.Arduino)3 ArrayList (java.util.ArrayList)2 JSONArray (org.json.JSONArray)2