Volley Overview
Volley is an HTTP library that makes networking for android
apps and most Important.
You have to find multiple customise example on google. But I
have explain keep and simple customise example, step by step.
Volley benefits
1.
Automatic scheduling of network request.
2.
Ease of customization for example.
3.
Support for request prioritization.
4.
Memory response caching with standard HTTP(cache
coherence).
5.
Debugging and tracing tools.
Volley is not suitable for large download or streaming
operation, since volley holds all response in memory during parsing. For large
download operation used DownloadManager.
Dependency:
Example How to call Volley :
Step 1. Add Dependency.
dependencies {
....
implementation 'com.android.volley:volley:1.1.1'
}
Step 2. Add permissions.
xml version="1.0" encoding="utf-8"?><
manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application ...
</application>
</manifest>
Step 3. Make Custom interface.
import com.android.volley.VolleyError;
public interface VolleyInterface {
public void onResponse(Object response);
public void onErrorResponse(VolleyError error);
}
Step 4. Make Volley call back .
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.mespl.volleywebintegration.MyApplication;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class VolleyResponse {
private static String BaseUrl = "http://" + "Your IP" + "/LoginApplication/";
public VolleyInterface anInterface;
private static String basicAut="";
public VolleyResponse(int methodType, String MethodName, JSONObject postParams) {
JsonObjectRequest request = new JsonObjectRequest(methodType,
BaseUrl + MethodName, postParams, new Response.Listener() {
@Override
public void onResponse(Object response) {
anInterface.onResponse(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
anInterface.onResponse(error);
}
})
};
//This is for Headers If You Needed
MyApplication.getAppInstance().addRequestQueue(request, "Request");
}
public void setAnInterface(VolleyInterface anInterface) {
this.anInterface = anInterface;
}
}
Step 5. Call service REST API.
a. You have add Dependency.
implementation 'com.google.code.gson:gson:2.3.1'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
b. You have to create a Request and Response model class and implement Serializable .
Request
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.io.Serializable;
public class LoginRequest implements Serializable {
@SerializedName("UserName")
@Expose private String UserName;
@SerializedName("Password")
@Expose private String Password;
@SerializedName("DeviceId")
@Expose private String DeviceId;
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String getPassword() {
return Password;
}
public void setPassword(String password) {
Password = password;
}
public String getDeviceId() {
return DeviceId;
}
public void setDeviceId(String deviceId) {
DeviceId = deviceId;
}
}
First you have make request function, suppose you want make a login request. make a two override functions.
private void setLogin() {
JSONObject obj = null;
LoginRequest loginRequest = new LoginRequest();
loginRequest.setDeviceId(Build.SERIAL);
loginRequest.setUserName("max");
loginRequest.setPassword("123");
// convert Gson to String
//String string = new Gson().toJson(loginRequest); try {
obj = new JSONObject(new Gson().toJson(loginRequest));
} catch (JSONException e) {
e.printStackTrace();
}
new VolleyResponse(Request.Method.POST, "GetLogin", obj).setAnInterface(this);
}
Implement
@Overridepublic void onResponse(Object response) {
String string = response.toString();
}
@Overridepublic void onErrorResponse(VolleyError error) {
}
Step 5. Make Main Activity.
import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.mespl.volleywebintegration.pojo.LoginRequest;
import com.mespl.volleywebintegration.volley.VolleyInterface;
import com.mespl.volleywebintegration.volley.VolleyResponse;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity implements VolleyInterface {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setLogin();
}
private void setLogin() {
JSONObject obj = null;
LoginRequest loginRequest = new LoginRequest();
loginRequest.setDeviceId(Build.SERIAL);
loginRequest.setUserName("max");
loginRequest.setPassword("123");
MyApplication.saveString("Username", loginRequest.getUserName());
MyApplication.saveString("Password", loginRequest.getPassword());
VolleyResponse.reStart();
//String string = new Gson().toJson(loginRequest); try {
obj = new JSONObject(new Gson().toJson(loginRequest));
} catch (JSONException e) {
e.printStackTrace();
}
new VolleyResponse(Request.Method.POST, "GetLogin", obj).setAnInterface(this);
}
@Override public void onResponse(Object response) {
String string = response.toString();
}
@Override public void onErrorResponse(VolleyError error) {
}
}
you have to Used Basic Authorization.