Search in sources :

Example 16 with StringRequest

use of com.android.volley.toolbox.StringRequest in project WordPress-Android by wordpress-mobile.

the class ReaderBlogActions method checkUrlReachable.

/*
     * tests whether the passed url can be reached - does NOT use authentication, and does not
     * account for 404 replacement pages used by ISPs such as Charter
     */
public static void checkUrlReachable(final String blogUrl, final ReaderActions.OnRequestListener requestListener) {
    // listener is required
    if (requestListener == null)
        return;
    Response.Listener<String> listener = new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            requestListener.onSuccess();
        }
    };
    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError volleyError) {
            AppLog.e(T.READER, volleyError);
            int statusCode;
            // since a redirect to an unauthorized url may return a 301 rather than a 401
            if (volleyError instanceof com.android.volley.AuthFailureError) {
                statusCode = 401;
            } else {
                statusCode = VolleyUtils.statusCodeFromVolleyError(volleyError);
            }
            // success since it means the blog url is reachable
            if (statusCode == 301) {
                requestListener.onSuccess();
            } else {
                requestListener.onFailure(statusCode);
            }
        }
    };
    // TODO: this should be a HEAD request, but even though Volley supposedly supports HEAD
    // using it results in "java.lang.IllegalStateException: Unknown method type"
    StringRequest request = new StringRequest(Request.Method.GET, blogUrl, listener, errorListener);
    WordPress.sRequestQueue.add(request);
}
Also used : Response(com.android.volley.Response) VolleyError(com.android.volley.VolleyError) UpdateBlogInfoListener(org.wordpress.android.ui.reader.actions.ReaderActions.UpdateBlogInfoListener) ActionListener(org.wordpress.android.ui.reader.actions.ReaderActions.ActionListener) StringRequest(com.android.volley.toolbox.StringRequest)

Example 17 with StringRequest

use of com.android.volley.toolbox.StringRequest 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);
}
Also used : VolleyError(com.android.volley.VolleyError) StringRequest(com.android.volley.toolbox.StringRequest) AsyncTask(android.os.AsyncTask) Request(com.android.volley.Request) StringRequest(com.android.volley.toolbox.StringRequest) JSONException(org.json.JSONException) IOException(java.io.IOException) DownloadManager(android.app.DownloadManager) Uri(android.net.Uri) URL(java.net.URL) IOException(java.io.IOException) JSONException(org.json.JSONException) Response(com.android.volley.Response) HttpURLConnection(java.net.HttpURLConnection) JSONObject(org.json.JSONObject) File(java.io.File)

Example 18 with StringRequest

use of com.android.volley.toolbox.StringRequest in project saga-android by AnandChowdhary.

the class SearchActivity method getSearchResults.

private void getSearchResults(final String query) {
    String url = "http://www.shazam.com/fragment/search/" + query.replace(" ", "%20") + ".json?size=medium";
    StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.d(TAG, response);
            try {
                mAdapter = new SearchAdapter(new JSONObject(response).getJSONArray("tracks"));
                mRecyclerView.setAdapter(mAdapter);
            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "No results found", Toast.LENGTH_SHORT).show();
                mSearchProgress.setVisibility(View.GONE);
                mClearButton.setVisibility(View.VISIBLE);
            }
            mSearchProgress.setVisibility(View.GONE);
            mClearButton.setVisibility(View.VISIBLE);
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(), getString(R.string.error_connect), Toast.LENGTH_SHORT).show();
            mSearchProgress.setVisibility(View.GONE);
            mClearButton.setVisibility(View.VISIBLE);
        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("q", query);
            return params;
        }
    };
    request.setShouldCache(false);
    mQueue.add(request);
}
Also used : Response(com.android.volley.Response) VolleyError(com.android.volley.VolleyError) JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) StringRequest(com.android.volley.toolbox.StringRequest) JSONException(org.json.JSONException)

Aggregations

StringRequest (com.android.volley.toolbox.StringRequest)18 VolleyError (com.android.volley.VolleyError)16 Response (com.android.volley.Response)14 JSONObject (org.json.JSONObject)5 JSONException (org.json.JSONException)4 Uri (android.net.Uri)3 Gson (com.google.gson.Gson)3 HashMap (java.util.HashMap)3 Request (com.android.volley.Request)2 RequestQueue (com.android.volley.RequestQueue)2 ImageLoader (com.android.volley.toolbox.ImageLoader)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 DownloadManager (android.app.DownloadManager)1 Intent (android.content.Intent)1 Bitmap (android.graphics.Bitmap)1 AsyncTask (android.os.AsyncTask)1 Nullable (android.support.annotation.Nullable)1 View (android.view.View)1 ImageButton (android.widget.ImageButton)1