How can i send image to server without name-value pair ? I know it is basic question but there is no clear answer about this in stackoverflow. How can we use -POST- method ? (As you know name-value pair is depreciated).
Asked
Active
Viewed 428 times
1
-
you can use namevalue pair if you want – Aditya Vyas-Lakhan Mar 02 '16 at 11:52
-
Check out my answer [here](http://stackoverflow.com/questions/29534340/missing-file-error-while-uploading-file-to-server-using-http-post#29624050). – Exception Mar 02 '16 at 11:53
-
http://stackoverflow.com/questions/29966762/upload-image-using-android-php/29967361#29967361 – Aditya Vyas-Lakhan Mar 02 '16 at 11:55
-
@Exception Http methods depreciated too – EsraMesra Mar 02 '16 at 12:00
-
You can send in multipart too. – Mar 02 '16 at 12:01
-
@EsraMesra, no try the code first. They are not depreciated. – Exception Mar 02 '16 at 12:12
-
Use a library for these kinds of operations unless you want to trouble yourself.. – Tim Mar 02 '16 at 12:15
-
Try using OkHttp and Retrofit. These libraries are very useful and simply these operations to a great extent. – Amit Tiwari Mar 02 '16 at 12:59
1 Answers
1
public String postPhotoUploadRequest(File imageFile) {
URL url;
String response = "";
int responseCode = -1;
try {
url = new URL("your url");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.addRequestProperty("Content-Type", "binary/octet-stream");
conn.setDoOutput(true);
conn.setReadTimeout(20000);
conn.setConnectTimeout(20000);
conn.setRequestMethod("POST");
DataOutputStream writer = new DataOutputStream(conn.getOutputStream());
Bitmap bmp = ImageUtils.filePathToBitmap(imageFile.getPath());
if(bmp != null) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 50, bos);
InputStream input = new ByteArrayInputStream(bos.toByteArray());
byte[] buffer = new byte[1024];
for (int length = 0; (length = input.read(buffer)) > 0; ) {
writer.write(buffer, 0, length);
}
writer.flush();
writer.close();
}
responseCode = conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = br.readLine()) != null) {
response += line;
}
} else {
response = "Response Code : " + responseCode;
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}

Febi M Felix
- 2,799
- 1
- 10
- 13