Last active
April 8, 2016 11:41
-
-
Save vipulasri/b96488c612c1f150a1da to your computer and use it in GitHub Desktop.
Android MultipartRequest using Volley with PHP code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Path to move uploaded files | |
$target_path = dirname(__FILE__).'/uploads/'; | |
if (isset($_FILES['image']['name'])) { | |
$target_path = $target_path . basename($_FILES['image']['name']); | |
try { | |
// Throws exception incase file is not being moved | |
if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) { | |
// make error flag true | |
echo json_encode(array('status'=>'fail', 'message'=>'could not move file')); | |
} | |
// File successfully uploaded | |
echo json_encode(array('status'=>'success', 'message'=>'File Uploaded')); | |
} catch (Exception $e) { | |
// Exception occurred. Make error flag true | |
echo json_encode(array('status'=>'fail', 'message'=>$e->getMessage())); | |
} | |
} else { | |
// File parameter is missing | |
echo json_encode(array('status'=>'fail', 'message'=>'Not received any file')); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.android.volley.AuthFailureError; | |
import com.android.volley.NetworkResponse; | |
import com.android.volley.Request; | |
import com.android.volley.Response; | |
import com.android.volley.VolleyLog; | |
import com.android.volley.toolbox.HttpHeaderParser; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
import com.google.gson.reflect.TypeToken; | |
import java.io.ByteArrayOutputStream; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.UnsupportedEncodingException; | |
import java.util.Map; | |
import cz.msebera.android.httpclient.HttpEntity; | |
import cz.msebera.android.httpclient.entity.ContentType; | |
import cz.msebera.android.httpclient.entity.mime.HttpMultipartMode; | |
import cz.msebera.android.httpclient.entity.mime.MultipartEntityBuilder; | |
import cz.msebera.android.httpclient.entity.mime.content.FileBody; | |
import cz.msebera.android.httpclient.util.CharsetUtils; | |
/** | |
* Created by HP-HP on 01-10-2015. | |
*/ | |
public class MultipartRequest<T> extends Request<T> { | |
private GsonBuilder gsonBuilder = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss"); | |
MultipartEntityBuilder entity = MultipartEntityBuilder.create(); | |
HttpEntity httpentity; | |
private String FILE_PART_NAME = "image"; | |
private final Response.Listener<T> mListener; | |
private final File mFilePart; | |
private TypeToken<T> typeToken; | |
private Gson gson = gsonBuilder.create(); | |
private final Map<String, String> mStringPart; | |
public MultipartRequest(String url, File file, Map<String, String> mStringPart, TypeToken<T> typeToken, ResponseListener<T> listener) { | |
super(Method.POST, url, listener); | |
this.mListener = listener; | |
this.mFilePart = file; | |
this.typeToken = typeToken; | |
this.mStringPart = mStringPart; | |
entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); | |
try { | |
entity.setCharset(CharsetUtils.get("UTF-8")); | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
} | |
buildMultipartEntity(); | |
httpentity = entity.build(); | |
} | |
private void buildMultipartEntity() { | |
entity.addPart(FILE_PART_NAME, new FileBody(mFilePart, ContentType.create("image/*"), mFilePart.getName())); | |
if (mStringPart != null) { | |
for (Map.Entry<String, String> entry : mStringPart.entrySet()) { | |
entity.addTextBody(entry.getKey(), entry.getValue()); | |
} | |
} | |
} | |
@Override | |
public String getBodyContentType() { | |
return httpentity.getContentType().getValue(); | |
} | |
@Override | |
public byte[] getBody() throws AuthFailureError { | |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
try | |
{ | |
httpentity.writeTo(bos); | |
} | |
catch (IOException e) | |
{ | |
VolleyLog.e("IOException writing to ByteArrayOutputStream"); | |
} | |
return bos.toByteArray(); | |
} | |
@Override | |
protected Response<T> parseNetworkResponse(NetworkResponse response) | |
{ | |
try { | |
String jsonResponse = new String(response.data, | |
HttpHeaderParser.parseCharset(response.headers)); | |
T newResponse = gson.fromJson(jsonResponse, typeToken.getType()); | |
return Response.success(newResponse, HttpHeaderParser.parseCacheHeaders(response)); | |
} catch (UnsupportedEncodingException e) { | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
@Override | |
protected void deliverResponse(T response) { | |
if(mListener != null) { | |
mListener.onResponse(response); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
yes give us implementation, it would help us.
Thanks a lot.