0

I have the following cURL that works in Bash:

$ curl -F "image=@/Users/my-name/Downloads/STARinside.jpg_256" -F "assetNumber=OS014005" -F "name=George" -F "make=Hamilton" -F "model=STAR" "http://exampleUrl.compute.amazonaws.com/Asmt3/updateInstrImage.php"

but I can't seem to quite get this working in Android Studio. here's some code I've tried, but I can't seem to get to work. I can get the POST of all the parameters themselves (including the image=@filename), but it appears the server (which is php that I wrote) is not receiving anything in $_FILES['image'] like it does from bash. Or I can send the actual file via a bufferedOutputStream, but then it doesn't capture anything meaningful in the $_POST array.

this code is in an extension of AsyncTask, FTR:

HttpURLConnection conn = (HttpURLConnection) this.updateInstr.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
//conn.setRequestProperty("Content-Type", "image/jpeg");
conn.setDoInput(true);
params.append("assetNumber=");
params.append(this.asset);
params.append("&name=");
params.append(InstrList.get(this.asset));
params.append("&make=");
params.append(InstrMakes.get(this.asset));
params.append("&model=");
params.append(InstrModels.get(this.asset));
params.append("&image=@");
params.append(image.getAbsoluteFile());
/*
    conn.addRequestProperty("assetNumber", this.asset);
    conn.addRequestProperty("name", InstrList.get(asset));
    conn.addRequestProperty("make", InstrMakes.get(asset));
    conn.addRequestProperty("model", InstrModels.get(asset));
*/
conn.setChunkedStreamingMode(bt.length);
OutputStream out = new BufferedOutputStream(conn.getOutputStream());
byte[] b = params.toString().getBytes(StandardCharsets.US_ASCII);
out.write(b);
out.flush();
out.write(bt);
out.flush();
out.close();

//fully open connection with conn.getInputStream
InputStream in = new BufferedInputStream(conn.getInputStream());
in.read(bytes);
in.close();
response = new String(bytes, "utf-8");
Log.d("response: ", response);

Also, you can see, I've tried a different way to conn.addRequestProperty. In that implementation, I tried writing b after bt (POST after FILE).

I definitely would appreciate help. It certainly seems I should be able to replicate this cURL call in Android Studio, but I just can't quite get it. I've added some echoing to the php on the server, so I know I'm assembling the params correctly (the $_POST array is correct based on a print_r($_POST) call), but the $_FILES array is empty from android studio (but is correct from bash-curl)

Quick note, I did find this other question which is very helpful and almost sufficient, but it appears that the ByteArrayBody class discussed for Java doesn't exist for Android Studio

Community
  • 1
  • 1
Keith
  • 777
  • 8
  • 27

1 Answers1

0

This turned out to be pretty involved, and I needed several different sources scattered around SO and elsewhere to (finally) get it working.

So, I have posted a very detailed example on my free-for-now amazon EC2 instance. if someone would like to take the source and post it elsewhere, I'd be happy for you to do so, provided you state the original author. For now:

http://ec2-54-213-243-1.us-west-2.compute.amazonaws.com/How-to/index.html

The gist is that you need to extend AsyncTask, but you also need to build the Http request character by character to delineate what is files (stream of bytes) vs 'normal' data fields.

the most involved part is: doInBackground() Which calls: writeText()

Keith
  • 777
  • 8
  • 27