Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ishmaelmakitla/12daa487c8d64439eaf5 to your computer and use it in GitHub Desktop.
Save ishmaelmakitla/12daa487c8d64439eaf5 to your computer and use it in GitHub Desktop.
package za.co.smartcitizens.trafficlights.spotter.utils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import za.co.smartcitizens.trafficlights.spotter.models.TrafficLight;
import za.co.smartcitizens.trafficlights.spotter.models.TrafficLightEncounter;
import za.co.smartcitizens.trafficlights.spotter.models.TrafficLightStatusReport;
import za.co.smartcitizens.trafficlights.spotter.models.TrafficReport;
import android.util.Log;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
/**
* This is a Multipart HTTP Request using the Volley library in Android. It is an extract from the code for the app that I have developed for
* reporting traffic conditions. The App is the Google Play Store:
* https://play.google.com/store/apps/details?id=za.co.smartcitizens.trafficlights.spotter
* Essentially this is an example of how to use multi-part request to send JSON-payload (as a part) -
* in this example I am also reusing the request based on which constructor was used to instantiate instance of this class.
*
* @author Ishmael Makitla, 2015
Country Mentor: Google Developer Groups South Africa,
GDG-Pretoria
*
*/
public class TrafficLightMultiPartSubmissionRequest extends Request<String> {
private static final String TAG = TrafficLightMultiPartSubmissionRequest.class.getSimpleName();
private MultipartEntityBuilder entity = MultipartEntityBuilder.create();
HttpEntity multiPartEntity;
//We are sending the traffic-light JSON data in a multi-part HTTP-Request to avoid having to send it like a file via upload
private static final String DATA_PART = "trafficLight";
private static final String ENCOUNTER_DATA_PART = "encounter";
private static final String INCIDENT_REPORT_DATA_PART = "report";
//this is the actual Traffic-Light payload
private TrafficLight mTrafficLight;
//this is the encountered traffic light
private TrafficLightEncounter mTrafficLightEncounter;
private Response.Listener<String> mListener;
Response.ErrorListener errorListener;
public TrafficLightMultiPartSubmissionRequest(int method, String url,ErrorListener listener) {
super(method, url, listener);
}
/**
* Overloaded Constructor for sending Traffic-Light spotting
* (this is when a user/motorist submits new location of a traffic-light intersection)
*/
public TrafficLightMultiPartSubmissionRequest(String url, Response.ErrorListener errorListener, Response.Listener<String> listener, TrafficLight trafficLight)
{
super(Method.POST, url, errorListener);
this.errorListener = errorListener;
this.mListener = listener;
this.mTrafficLight = trafficLight;
//build an appropriate entity for the request
try
{ //check that we have data to send
entity.addPart(DATA_PART, new StringBody(mTrafficLight.toString(), ContentType.TEXT_PLAIN));
entity.setBoundary("-------------------");
multiPartEntity = entity.build();
}
catch (Exception e)
{
VolleyLog.e("UnsupportedEncodingException");
}
}
/**
* Overloaded Constructor for sending Traffic-Light Status (Encounter) Reports
*/
public TrafficLightMultiPartSubmissionRequest(String url, Response.ErrorListener errorListener, Response.Listener<String> listener, TrafficLightEncounter trafficLightEncounter)
{
super(Method.POST, url, errorListener);
this.errorListener = errorListener;
this.mListener = listener;
this.mTrafficLightEncounter = trafficLightEncounter;
try
{ //check that we have data to send
entity.addPart(ENCOUNTER_DATA_PART, new StringBody(mTrafficLightEncounter.toString(), ContentType.TEXT_PLAIN));
entity.setBoundary("-------------------");
multiPartEntity = entity.build();
}
catch (Exception e)
{
VolleyLog.e("UnsupportedEncodingException");
}
}
/**
* Overloaded Constructor for sending General Traffic Incident Report
*/
public TrafficLightMultiPartSubmissionRequest(String url, Response.ErrorListener errorListener, Response.Listener<String> listener, TrafficReport trafficIncidentReport)
{
super(Method.POST, url, errorListener);
this.errorListener = errorListener;
this.mListener = listener;
try
{ //check that we have data to send
entity.addPart(INCIDENT_REPORT_DATA_PART, new StringBody(trafficIncidentReport.toString(), ContentType.TEXT_PLAIN));
entity.setBoundary("-------------------");
multiPartEntity = entity.build();
}
catch (Exception e)
{
VolleyLog.e("UnsupportedEncodingException");
}
}
@Override
public String getBodyContentType()
{ String entityString = multiPartEntity.getContentType().getValue();
VolleyLog.v(TAG, entityString);
return entityString;
}
@Override
public byte[] getBody() throws AuthFailureError
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try
{
multiPartEntity.writeTo(bos);
}
catch (IOException e)
{ e.printStackTrace();
VolleyLog.e("IOException writing to ByteArrayOutputStream", e);
}
return bos.toByteArray();
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response)
{ String serverResponse = new String(response.data);
return Response.success(serverResponse, getCacheEntry());
}
@Override
protected void deliverResponse(String response)
{
mListener.onResponse(response);
}
@Override
public void deliverError(VolleyError error) {
errorListener.onErrorResponse(error);
}
@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
Log.e(TAG, "Volley Error", volleyError);
return super.parseNetworkError(volleyError);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment