1

Would anyone be able to help convert the following cURL command to a HTTP POST which can be run from a button click on an Android App?

cURL Command:

'C:\\DevPrograms\\curl-7.59.0-win32-mingw\\bin\\curl.exe' -X POST --data-binary '@C:\xampp\htdocs\Pastec_Test_Connection\Test_Images\Test_FiveHundredEuro01.jpg' 'http://localhost:4212/index/searcher'
Jay1995
  • 137
  • 1
  • 14
  • 1
    Possible duplicate of [Sending POST data in Android](https://stackoverflow.com/questions/2938502/sending-post-data-in-android) – David Apr 14 '18 at 16:23
  • Please do not post the same question within two hours https://stackoverflow.com/questions/49832174/android-is-it-possible-to-send-a-command-from-an-android-app-to-a-web-page-to-t?noredirect=1#comment86681144_49832174 – greenapps Apr 14 '18 at 18:45

1 Answers1

0

Given below are the codes to:

  • Load a JPG as bitmap.
  • Convert the bitmap into Byte Array.
  • Sending a POST through HttpURLConnection with Byte Array.
  • Getting the response from server.

Bitmap bitmap = null;
Uri uri = Uri.fromFile(new File("C:/xampp/htdocs/Pastec_Test_Connection/Test_Images/Test_FiveHundredEuro01.jpg"));
InputStream stream = null;

try {
    stream = getContentResolver().openInputStream(uri);
    BitmapFactory.Options options = new BitmapFactory.Options();
    bitmap = BitmapFactory.decodeStream(stream, null, options);
} catch (IOException e) {
    Log.e("IOException", e.getMessage(), e);
    return null;
} finally {
    try {
        if (stream != null) stream.close();
    } catch (Exception e) {}
}
ByteArrayOutputStream byteStream = null;
byteStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteStream);
// Convert ByteArrayOutputStream to byte array. Close stream. 
byte[] byteArray = byteStream.toByteArray();
byteStream.close();
byteStream = null;

URL url = null;
try {
    url = new URL("http://localhost:4212/index/searcher");
} catch (MalformedURLException e) {
    e.printStackTrace();
}
HttpURLConnection conn = null;
try {
    conn = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
    e.printStackTrace();
}
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(false);
try {
    conn.setRequestMethod("POST");
} catch (ProtocolException e) {
    e.printStackTrace();
}
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("charset", "utf-8");
conn.setRequestProperty("Content-Length", Integer.toString(byteArray.length));
conn.setUseCaches(false);
try {
    DataOutputStream wr = new DataOutputStream(conn.getOutputStream()) {
        wr.write(byteArray);
        wr.flush();
        wr.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    //Get Response  
    InputStream is = connection.getInputStream();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));
    String line;
    StringBuffer response = new StringBuffer();
    while ((line = rd.readLine()) != null) {
        response.append(line);
        response.append('\r');
    }
    rd.close();
    System.out.println(response.toString());
Midhun Monachan
  • 588
  • 4
  • 12