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));
}
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);
}
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));
}
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);
}
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);
}
Aggregations