Downloading the file on the button, how do you do?



  • We need to do this: When the user pushes the button down, the file is downloaded. If there's no Internet, it's a message. How do you do it?



  • Create the AppController file.

    import android.app.Application;
    import android.text.TextUtils;
    

    import com.android.volley.Request;
    import com.android.volley.RequestQueue;
    import com.android.volley.toolbox.Volley;

    public class AppController extends Application {

    public static final String TAG = AppController.class.getSimpleName();
    
    private RequestQueue mRequestQueue;
    
    private static AppController mInstance;
    
    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }
    
    public static synchronized AppController getInstance() {
        return mInstance;
    }
    
    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }
    
        return mRequestQueue;
    }
    
    public <T> void addToRequestQueue(Request<T> req, String tag) {
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }
    

    }

    Adding to the manifest

    <application
    android:name=".AppController"
    ...
    />

    B build.gradle add

    dependencies {
    ...
    compile 'com.mcxiaoke.volley:library-aar:1.0.0'
    }

    And finally, it's asynchronous, so you don't have to give him AsyncTask or anything below the line, you can also create a method for another file. http://www.techstricks.com/download-file-using-android-volley ♪

    String tag_string_req = "request";
    final ProgressDialog progressDialog = new ProgressDialog(getContext());
    progressDialog.setMessage("Подключение к серверу. Подождите...");
    progressDialog.setIndeterminate(false);
    progressDialog.setCancelable(false);
    progressDialog.show();
    StringRequest strReq = new StringRequest(Request.Method.POST, url,
    new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
    progressDialog.dismiss();
    Log.d("response",response);
    }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

            progressDialog.dismiss();
            AlertDialog.Builder alert = new AlertDialog.Builder(getContext());
            alert.setTitle("Ошибка авторизации");
            final TextView input = new TextView(getContext());
            alert.setView(input);
            input.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
            input.setPadding(20, 10, 20, 10);
            input.setTextColor(ContextCompat.getColor(getContext(), android.R.color.black));
            input.setText("Ошибка");
            alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                }
            });
    
            alert.show();
        }
    }) {
    
        @Override
        protected Map&lt;String, String&gt; getParams() {
            //Записываем параметры в запрос который передаем на сервер
            Map&lt;String, String&gt; params = new HashMap&lt;&gt;();
            params.put("param", param);
            return params;
        }
    
    };
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
    




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2