Skip to content

Instantly share code, notes, and snippets.

package net.colaborativa.exampleapp.api.volley;
import java.io.UnsupportedEncodingException;
import java.util.Map;
import android.util.Log;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
package net.colaborativa.exampleapp.api;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@mbarcia
mbarcia / CachedStringGETRequest.java
Last active January 4, 2016 18:29
Further adapt a Volley Request by specifying GET parameter, Json response decoding, and cache and retry policies. Part of http://develop-for-android.blogspot.com.es/2014/01/using-volley-in-your-application.html
package net.colaborativa.exampleapp.api.volley;
import java.io.UnsupportedEncodingException;
import com.android.volley.Cache;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
@mbarcia
mbarcia / addingRequest.txt
Last active January 4, 2016 21:29
Creating an instance of a Volley request by means of an adapter class, and adding it to the queue for execution. Part of http://develop-for-android.blogspot.com.es/2014/01/using-volley-in-your-application_28.html
String baseUrl =
"http://www.webservicex.net/currencyconvertor.asmx/ConversionRate";
// using the cached request
Request<String> request = new CachedStringGETRequest(
baseUrl + "?fromCurrencyCode=USD&toCurrencyCode=EUR",
new ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.w("exampleapp", "Exchange rate USD " +
"to EUR failed to update. " + error );
@mbarcia
mbarcia / inner-listener.java
Created January 29, 2014 16:23
Adding business logic in the listener
new Listener<String>() {
@Override
public void onResponse(String response) {
MyItem item = new MyItem(response);
display(item);
}
package net.colaborativa.exampleapp;
import android.content.Context;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.Volley;
@mbarcia
mbarcia / AuthenticatorActivity.java
Created January 31, 2014 17:17
Handle login event using MyApi Request factory, a "custom" listener and an error listener. Part of http://develop-for-android.blogspot.com.es/2014/01/using-volley-in-your-application_31.html
// ...
/**
* Handles onClick event on the Submit button. Sends username/password to
* the server for authentication.
*
* @param view The Submit button for which this method is invoked
*/
public void handleLogin(View view) {
if(((EditText) findViewById(R.id.username)).getText() == null ||
package net.colaborativa.exampleapp;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.text.TextUtils;
import android.util.Log;
// This is the login activity of your app, only used
// here to retrieve the value of ACCOUNT_TYPE
import net.colaborativa.exampleapp.account.AuthenticatorActivity;
package net.colaborativa.exampleapp;
// check your imports, this list may not be complete
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.accounts.OnAccountsUpdateListener;
import android.content.Intent;
import android.text.TextUtils;
@mbarcia
mbarcia / postImage
Created November 25, 2014 22:07
postImage() you can add to your MyApi class. Read more about a multipart request at http://stackoverflow.com/a/9082243
public Request<?> postImage(
String token,
long l,
File imageFile,
Listener<String> listener,
ErrorListener errorListener) {
String url = "http://yoururl.com/post-image";
PhotoMultipartRequest<String> req = new PhotoMultipartRequest<String>(