use of com.android.volley.VolleyError in project TaEmCasa by Dionen.
the class BasicNetworkTest method socketTimeout.
@Test
public void socketTimeout() throws Exception {
MockHttpStack mockHttpStack = new MockHttpStack();
mockHttpStack.setExceptionToThrow(new SocketTimeoutException());
BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
Request<String> request = buildRequest();
request.setRetryPolicy(mMockRetryPolicy);
doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
try {
httpNetwork.performRequest(request);
} catch (VolleyError e) {
// expected
}
// should retry socket timeouts
verify(mMockRetryPolicy).retry(any(TimeoutError.class));
}
use of com.android.volley.VolleyError in project TaEmCasa by Dionen.
the class BasicNetworkTest method noConnection.
@Test
public void noConnection() throws Exception {
MockHttpStack mockHttpStack = new MockHttpStack();
mockHttpStack.setExceptionToThrow(new IOException());
BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
Request<String> request = buildRequest();
request.setRetryPolicy(mMockRetryPolicy);
doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
try {
httpNetwork.performRequest(request);
} catch (VolleyError e) {
// expected
}
// should not retry when there is no connection
verify(mMockRetryPolicy, never()).retry(any(VolleyError.class));
}
use of com.android.volley.VolleyError in project TaEmCasa by Dionen.
the class BasicNetworkTest method redirect.
@Test
public void redirect() throws Exception {
for (int i = 300; i <= 399; i++) {
MockHttpStack mockHttpStack = new MockHttpStack();
BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), i, "");
mockHttpStack.setResponseToReturn(fakeResponse);
BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
Request<String> request = buildRequest();
request.setRetryPolicy(mMockRetryPolicy);
doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
try {
httpNetwork.performRequest(request);
} catch (VolleyError e) {
// expected
}
// should not retry 300 responses.
verify(mMockRetryPolicy, never()).retry(any(VolleyError.class));
reset(mMockRetryPolicy);
}
}
use of com.android.volley.VolleyError in project TaEmCasa by Dionen.
the class BasicNetworkTest method unauthorized.
@Test
public void unauthorized() throws Exception {
MockHttpStack mockHttpStack = new MockHttpStack();
BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 401, "Unauthorized");
mockHttpStack.setResponseToReturn(fakeResponse);
BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
Request<String> request = buildRequest();
request.setRetryPolicy(mMockRetryPolicy);
doThrow(new VolleyError()).when(mMockRetryPolicy).retry(any(VolleyError.class));
try {
httpNetwork.performRequest(request);
} catch (VolleyError e) {
// expected
}
// should retry in case it's an auth failure.
verify(mMockRetryPolicy).retry(any(AuthFailureError.class));
}
use of com.android.volley.VolleyError in project aplicativo by InCasa.
the class AdminActivity method atualizar.
public void atualizar(JSONObject json, String PUTUSER) {
User user = User.getInstancia();
PUTUSER = PUTUSER + user.getId();
JsonObjectRequest req = new JsonObjectRequest(Request.Method.PUT, PUTUSER, json, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Context context = getApplicationContext();
CharSequence text = "Administrador: Dados atualizados com sucesso !";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Intent it = new Intent(AdminActivity.this, LoginActivity.class);
startActivity(it);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Context context = getApplicationContext();
CharSequence text = "Administrador: Falha ao atualizar !";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
// add headers <key,value>
User user = User.getInstancia();
String auth = new String(Base64.encode((user.getLogin() + ":" + user.getSenha()).getBytes(), Base64.DEFAULT));
headers.put("Authorization ", " Basic " + auth);
return headers;
}
};
// Add the request to the RequestQueue.
// fila de requisições
RequestQueue fila = Volley.newRequestQueue(this);
// Adiciona a requisição á fila de requisições
fila.add(req);
}
Aggregations