Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save neelratanguria/acfd3f890024bc08f6a6e1bf38859ee5 to your computer and use it in GitHub Desktop.
Save neelratanguria/acfd3f890024bc08f6a6e1bf38859ee5 to your computer and use it in GitHub Desktop.
public class ReportGrievanceRequest {
// Initialize the objects
Context context;
ReportGrievanceCallback reportGrievanceCallback;
// Call Constructor to fetch objects
public ReportGrievanceRequest(Context context, ReportGrievanceCallback reportGrievanceCallback) {
this.context = context;
this.reportGrievanceCallback = reportGrievanceCallback;
}
// Send the data request from user to server
public void sendRequest(
final String title,
final String category,
final String description,
final String filename1,
final String filename2,
final String filename3,
final String filename4,
final String filename5,
String authToken
)
{
// Create a url for API
String url = urlBuilder(Constants.BASE_URL, API.REPORT_GRIEVANCE);
// Call a new volley request
RequestQueue mRequestQueue = Volley.newRequestQueue(context);
// Call a new json object to fetch data with method Post
VolleyMultipartRequest volleyMultipartRequest = new VolleyMultipartRequest(Request.Method.POST, url,
new Response.Listener<NetworkResponse>() {
@Override
public void onResponse(NetworkResponse response) {
try {
// Create a new Json object
JSONObject responseObject = new JSONObject(new String(response.data));
// If response is success upload the data into server
if (responseObject.getBoolean("success"))
{
reportGrievanceCallback.onSuccess();
}
else
{
// Show error when fail to fetch
reportGrievanceCallback.onFailure();
}
} catch (JSONException e) {
e.printStackTrace();
// Show error when fail to fetch
reportGrievanceCallback.onFailure();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Show error when fail to fetch
reportGrievanceCallback.onFailure();
}
})
{
/*
* If you want to add more parameters with the image
* you can do it here
* here we have only one parameter with the image
* which is tags
* */
protected Map<String, File> stringFileMap(){
Map<String, File> params = new HashMap<>();
// Set images in side filename
// params.put("image1", filename1);
// params.put("image2", filename2);
// params.put("image3", filename3);
return params;
}
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
// Set data into respective value
params.put("title", title);
params.put("description", description);
params.put("category", category);
params.put("image1", filename1);
params.put("image2", filename2);
params.put("image3", filename3);
params.put("image4", filename4);
params.put("image5", filename5);
return params;
}
/*
* Here we are passing image by renaming it with a unique name
* */
@Override
protected Map<String, DataPart> getByteData() {
Map<String, DataPart> params = new HashMap<>();
long imageName = System.currentTimeMillis();
// params.put("image", new DataPart(imageName + ".png", getFileDataFromDrawable(image1)));
return params;
}
@Override
public Map<String, String> getHeaders() {
Map<String, String> params = new HashMap<>();
// Send a new token while fetch data
params.put("Authorization","Bearer "+ authToken);
return params;
}
};
mRequestQueue.add(volleyMultipartRequest);
}
// Interface to fetch when new call back function call
public interface ReportGrievanceCallback
{
void onSuccess();
void onFailure();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment