Java tutorial
/* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.intuit.elves.network.mock; import com.android.volley.AuthFailureError; import com.android.volley.NetworkResponse; import com.android.volley.Response; import com.android.volley.Response.ErrorListener; import com.android.volley.VolleyError; import com.intuit.elves.network.CustomNetworkResponse; import com.intuit.elves.network.CustomRequest; import com.intuit.elves.network.NetworkUtil; import org.apache.http.HttpEntity; import org.apache.http.HttpRequest; import org.apache.http.entity.StringEntity; import org.apache.http.protocol.HTTP; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; public class MockRequest extends CustomRequest<String> { public static final String CUSTOM_HEADER = "customheader"; public static final String CUSTOM_HEADER_VALUE = "customheader_value"; private Response.Listener<String> mListener; @Override protected void onPrepareRequest(Object request) throws IOException, AuthFailureError { if (request instanceof HttpRequest) { ((HttpRequest) request).addHeader(CUSTOM_HEADER, CUSTOM_HEADER_VALUE); } super.onPrepareRequest(request); } public static final String REQUEST_BODY = "test"; public static final String JSON_CONTENT_TYPE = String.format("application/json"); public MockRequest() { super(Method.POST, "http://foo.com", null); } public MockRequest(String url, ErrorListener listener) { super(Method.POST, url, listener); } public MockRequest(String url, Response.Listener<String> listener, ErrorListener errorListener) { super(Method.POST, url, errorListener); mListener = listener; } public MockRequest(String url, int method, Response.Listener<String> listener, ErrorListener errorListener) { super(method, url, errorListener); mListener = listener; } public boolean deliverResponse_called = false; public boolean parseResponse_called = false; public boolean deliverError_called = false; @Override public void deliverError(VolleyError error) { deliverError_called = true; super.deliverError(error); } public boolean cancel_called = false; @Override public void cancel() { cancel_called = true; super.cancel(); } @Override protected Response<String> parseNetworkResponse(NetworkResponse response) { parseResponse_called = true; return Response.success( NetworkUtil.getResponsePayloadAsString(((CustomNetworkResponse) response).getEntity()), null); } @Override protected HttpEntity getEntity() { try { return new StringEntity(REQUEST_BODY, HTTP.UTF_8); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override protected void deliverResponse(String response) { deliverResponse_called = true; if (mListener != null) { mListener.onResponse(response); } } @Override public String getResponseContentType() { return JSON_CONTENT_TYPE; } @Override public String getBodyContentType() { return JSON_CONTENT_TYPE; } public static String getResponsePayloadAsString(HttpEntity entity) { InputStream is = null; String responseString = ""; try { is = entity.getContent(); if (is != null) { final StringBuilder sb = new StringBuilder(); String line; final BufferedReader reader = new BufferedReader(new InputStreamReader(is, HTTP.UTF_8)); while ((line = reader.readLine()) != null) { if (sb.length() > 0) { sb.append('\n'); } sb.append(line); } responseString = sb.toString(); } } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return responseString; } }