SQLDIAG-Configuration File-Extension

2023-01-12   ES  

Use the Volley framework. We first need to define a message request queue to initialize in the Application and open it to the outside world.

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import android.app.Application;
public class MyApplication extends Application {
    
// We need to use Volley's jar package
    private static RequestQueue queues;

    @Override
    public void onCreate() {
        super.onCreate();
        queues=Volley.newRequestQueue(getApplicationContext());
    }

    // Now we need to provide an external method to request our request queue
    public static RequestQueue getHttpQueues(){
        return queues;
    }

}

The code above needs to be introduced in our list file application label

The implementation code in Application below

<application
        android:name="com.example.volley.MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

Finally let us walk into the Volley framework GET Post request. In the implementation code, we provide two methods. The first is the StringRequest request method. The second is the JSONObjectRequest request method. JSONObjectRequest is very similar

import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
import com.android.volley.Request.Method;
import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends ActionBarActivity {
    
    // First of all, you need to define the queue of a global message request to let us define an application
    // We know that there are three request methods for the Volley framework.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Implement GET request
        Volley_GET();
        Volley_POST();

    }
    /** 
      * Post request 
      */
    private void Volley_POST() {
        String url="";
        // The first thing that needs to be implemented is StringRequest request data
        StringRequest request=new StringRequest(Method.POST,url, new Listener<String>() {

            @Override
            public void onResponse(String response) {
                // Here we just achieve the printing
                Log.i("TAG",response);
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i("TAG", error.toString());
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> hasMap=new HashMap<String,String>();
                hasMap.put("", "");
                hasMap.put("", "");
                // This realizes our parameters
                return hasMap;
            }
        };

        // Set a TAG for the request
        request.setTag("POSTREQUEST");
        // Add to the message queue
        MyApplication.getHttpQueues().add(request);


        // Let the JSONObjectRequest request below
        Map<String, String> hasMap=new HashMap<String,String>();
        hasMap.put("", "");
        hasMap.put("", "");
        JSONObject object=new JSONObject(hasMap);
        JsonObjectRequest jSONRequest=new JsonObjectRequest(Method.POST,url, object,new Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                // Now we just print the content of the request
                Log.i("TAG", response.toString());
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i("TAG", error.toString());
            }
        });
        // Set a tag
    jSONRequest.setTag("JSONOBJECT");
    MyApplication.getHttpQueues().add(jSONRequest);

    }

    private void Volley_GET() {
        String url="";
        // First of all, what we need is the way of request data. We choose StringRequest
        StringRequest request=new StringRequest(Method.GET,url, new Listener<String>() {

            @Override
            public void onResponse(String response) {
                // We can print the request information
                Log.i("TAG", response);
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i("TAG", error.toString());
            }
        });
        // Set a tag
        request.setTag("GETREQUEST");
        // Add this request to the request queue of the message
        MyApplication.getHttpQueues().add(request);



        // For get requests, we don’t need parameters, so we pass in a null
        // Below we use JSONObjectRequest to request
        JsonObjectRequest jSONRequest=new JsonObjectRequest(Method.GET,url, null,new Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                // Now we just print the content of the request
                Log.i("TAG", response.toString());
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i("TAG", error.toString());
            }
        });
        // Set a tag
        jSONRequest.setTag("JSONREQUEST");
        // Add to the message queue
        MyApplication.getHttpQueues().add(jSONRequest);
    }


    // Volley's request and a linkage with the Activity life cycle
    @Override
    protected void onStop() {
        super.onStop();
        MyApplication.getHttpQueues().cancelAll("JSONREQUEST");// Through the given tag value, all the specified queues are turned off
    }

}

In the above code, we also realized that the cancellation of the request queue is to set up a TAG for our request. In the current Activity stop life cycle, we cancel the request. Refer to the above code here. Forgot to add permissions to connect the network

source

Related Posts

FLEX layout dictionary

Detailed explanation SpringBoot Schedule configuration Amelia

Caption that Java thread is necessary for dead locks

[Japanese] Common Japanese sessions

SQLDIAG-Configuration File-Extension

Random Posts

SpringBoot and JWT integration

Python list commonly used operation The

ES command summary

Talk about Vue component welkin

Redhat Linux 6 Install notes