use of com.android.volley.Request in project iosched by google.
the class BasicNetworkTest method testHeadersAndPostParams.
public void testHeadersAndPostParams() throws Exception {
MockHttpStack mockHttpStack = new MockHttpStack();
BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 200, "OK");
fakeResponse.setEntity(new StringEntity("foobar"));
mockHttpStack.setResponseToReturn(fakeResponse);
BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
Request<String> request = new Request<String>(Request.Method.GET, "http://foo", null) {
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
return null;
}
@Override
protected void deliverResponse(String response) {
}
@Override
public Map<String, String> getHeaders() {
Map<String, String> result = new HashMap<String, String>();
result.put("requestheader", "foo");
return result;
}
@Override
public Map<String, String> getParams() {
Map<String, String> result = new HashMap<String, String>();
result.put("requestpost", "foo");
return result;
}
};
httpNetwork.performRequest(request);
assertEquals("foo", mockHttpStack.getLastHeaders().get("requestheader"));
assertEquals("requestpost=foo&", new String(mockHttpStack.getLastPostBody()));
}
use of com.android.volley.Request in project android-volley by mcxiaoke.
the class BasicNetworkTest method headersAndPostParams.
@Test
public void headersAndPostParams() throws Exception {
MockHttpStack mockHttpStack = new MockHttpStack();
BasicHttpResponse fakeResponse = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 200, "OK");
fakeResponse.setEntity(new StringEntity("foobar"));
mockHttpStack.setResponseToReturn(fakeResponse);
BasicNetwork httpNetwork = new BasicNetwork(mockHttpStack);
Request<String> request = new Request<String>(Request.Method.GET, "http://foo", null) {
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
return null;
}
@Override
protected void deliverResponse(String response) {
}
@Override
public Map<String, String> getHeaders() {
Map<String, String> result = new HashMap<String, String>();
result.put("requestheader", "foo");
return result;
}
@Override
public Map<String, String> getParams() {
Map<String, String> result = new HashMap<String, String>();
result.put("requestpost", "foo");
return result;
}
};
httpNetwork.performRequest(request);
assertEquals("foo", mockHttpStack.getLastHeaders().get("requestheader"));
assertEquals("requestpost=foo&", new String(mockHttpStack.getLastPostBody()));
}
use of com.android.volley.Request in project WordPress-Android by wordpress-mobile.
the class ReaderPostActions method bumpPageViewForPost.
public static void bumpPageViewForPost(SiteStore siteStore, ReaderPost post) {
if (post == null) {
return;
}
// don't bump stats for posts in sites the current user is an admin of, unless
// this is a private post since we count views for private posts from owner or member
SiteModel site = siteStore.getSiteBySiteId(post.blogId);
// site will be null here if the user is not the owner or a member of the site
if (site != null && !post.isPrivate) {
AppLog.d(T.READER, "skipped bump page view - user is admin");
return;
}
Response.Listener<String> listener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
AppLog.d(T.READER, "bump page view succeeded");
}
};
Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
AppLog.e(T.READER, volleyError);
AppLog.w(T.READER, "bump page view failed");
}
};
Request request = new StringRequest(Request.Method.GET, getTrackingPixelForPost(post), listener, errorListener) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
// call will fail without correct refer(r)er
Map<String, String> headers = new HashMap<>();
headers.put("Referer", TRACKING_REFERRER);
return headers;
}
};
WordPress.sRequestQueue.add(request);
}
use of com.android.volley.Request in project saga-android by AnandChowdhary.
the class MusicDownloader method startDownload.
public static void startDownload(final Context context, final String songName, final String artistName, final DownloaderListener listener) {
listener.showProgressBar();
String url = "http://162.243.144.151/new_api.php?q=" + songName.replace(" ", "%20");
if (artistName != null) {
url += "&r=" + artistName.replace(" ", "%20");
}
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.length() != 0) {
try {
final String[] fileName = new String[1];
final JSONObject jsonObject = new JSONObject(response);
String BASE_URL = "http://YouTubeInMP3.com/fetch/?video=http://www.youtube.com/watch?v=";
final DownloadManager dMgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
final String urlString = BASE_URL + jsonObject.getString("id");
final URL url = new URL(urlString);
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... voids) {
try {
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setInstanceFollowRedirects(true);
return urlConnection.getContentType();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
Log.d("Content type: ", result);
if ("audio/mpeg".equals(result)) {
listener.hideProgressBar();
Uri uri = Uri.parse(urlString);
DownloadManager.Request dr = new DownloadManager.Request(uri);
if (artistName == null) {
try {
fileName[0] = jsonObject.getString("title");
} catch (JSONException e) {
e.printStackTrace();
}
fileName[0].replaceAll("(?i)\\b(official|lyrics|lyric|video|song)\\b", "");
fileName[0].trim().replaceAll(" +", " ");
} else {
fileName[0] = songName;
}
fileName[0] += ".mp3";
dr.setTitle(fileName[0]);
dr.setDestinationUri(Uri.fromFile(new File(Utils.getStoragePath(context) + "/" + fileName[0])));
dMgr.enqueue(dr);
Toast.makeText(context, "Downloading...", Toast.LENGTH_SHORT).show();
listener.onSuccess();
getSongInfo(context, fileName[0].substring(0, fileName[0].length() - 4), songName, artistName);
} else {
listener.hideProgressBar();
Toast.makeText(context, "Nothing found, sorry. Try again later", Toast.LENGTH_SHORT).show();
}
}
}.execute();
} catch (Exception e) {
e.printStackTrace();
}
} else {
listener.hideProgressBar();
Toast.makeText(context, "Nothing found, sorry. Try again later", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context, context.getString(R.string.error_connect), Toast.LENGTH_SHORT).show();
listener.hideProgressBar();
}
});
request.setShouldCache(false);
VolleySingleton.getInstance(context).getRequestQueue().add(request);
}
Aggregations