I want to upload image to server form my android application so below is my code for that
List<NameValuePair> nameValuePairs;
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("email", "abc@mail.com"));
nameValuePairs.add(new BasicNameValuePair("password", "123"));
nameValuePairs.add(new BasicNameValuePair("first_name", "test"));
nameValuePairs.add(new BasicNameValuePair("last_name", "test"));
nameValuePairs.add(new BasicNameValuePair("mobile_no", "1234567890"));
nameValuePairs.add(new BasicNameValuePair("pincode", "12345"));
nameValuePairs.add(new BasicNameValuePair("address", "122"));
nameValuePairs.add(new BasicNameValuePair("old_image", "abc.png"));
nameValuePairs.add(new BasicNameValuePair("image", "/mnt/sdcard/AppFolder/1457068698592.png"));
Call web API for above post is
public Object postHttpURLWithImage(String url, List<NameValuePair> nameValuePairs, Object mObject) {
HttpPost httppost;
HttpParams httpParameters;
int timeoutConnection = 60000;
HttpClient httpclient = null;
HttpResponse response = null;
String data = "";
isOtherError = false;
System.out.println("Url " + url);
mFillObject = null;
if (check_Internet()) {
try {
mFillObject = mObject.getClass().newInstance();
httppost = new HttpPost(url);
httppost.addHeader("Mobile-Tokon", "7c^4N:9Y*Tq;P^f");
httppost.addHeader("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundaryc95nBhnBzMGoRjV4");
httppost.addHeader("X-Requested-With", "XMLHttpRequest");
httppost.addHeader("Accept", "*/*");
httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
httpclient = new DefaultHttpClient(httpParameters);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
if (nameValuePairs != null) {
for (int index = 0; index < nameValuePairs.size(); index++) {
String paramName = nameValuePairs.get(index).getName();
String paramValue = nameValuePairs.get(index).getValue();
System.out.println("paramName " + paramName);
System.out.println("paramValue " + paramValue);
if (paramName.equalsIgnoreCase("image")) {
if (paramValue.length() > 0) {
entity.addPart(paramName, new FileBody(new File(paramValue)));
}
} else {
entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));
}
}
// httppost.setEntity(entity);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}
// Execute HTTP Post Request
response = httpclient.execute(httppost);
System.out.println("httppost " + httppost.toString());
data = EntityUtils.toString(response.getEntity());
System.out.println("Final Data " + data);
mStringresponse = data;
if (data.equalsIgnoreCase("{\"is_device_deleted\":true}"))
setDeviceToken(true);
mFillObject = mGson.fromJson(data, mFillObject.getClass());
} catch (Exception e) {
isOtherError = true;
}
}
return mFillObject;
}
When I run above code my image is not uploaded to server so any idea how can I solve this ? your all suggestions are appreciable
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1TucnlP
via IFTTT
Aucun commentaire:
Enregistrer un commentaire