5

I am trying to upload an image to a PHP page along with some other info about the image so the PHP page knows what to do with it. Currently, I am getting away with it using this:

URL url = new URL("http://www.tagverse.us/upload.php?authcode="+WEB_ACCESS_CODE+"&description="+description+"&userid="+userId);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");

OutputStream os = connection.getOutputStream();
InputStream is = mContext.getContentResolver().openInputStream(uri);
BufferedInputStream bis = new BufferedInputStream(is);
int totalBytes = bis.available();

for(int i = 0; i < totalBytes; i++) {
    os.write(bis.read());
}
os.close();

BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String serverResponse = "";
String response = "";

while((response = reader.readLine()) != null) {
    serverResponse = serverResponse + response;
}
reader.close();
bis.close();

Is there a more elegant solution to this besides having a GET/POST hybrid? I feel as if this is sloppy, but for all I know it is a perfectly acceptable solution. If there is a better way of doing this, I'd appreciate being pointed in the right direction. Thanks!

PS: I am familiar with how you would, under normal conditions, interact with a PHP page via POST:

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://www.tagverse.us/login.php");

try {
    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    pairs.add(new BasicNameValuePair("authcode", WEB_ACCESS_CODE));
    pairs.add(new BasicNameValuePair("username", username));
    pairs.add(new BasicNameValuePair("password", password));
    post.setEntity(new UrlEncodedFormEntity(pairs));
    client.execute(post);
}

Essentially what I'd like to do is combine these two methods, but because I work with an HttpURLConnection object rather than a HttpPost object, it isn't as simple as just merging the two.

Thank you!

JMRboosties
  • 15,500
  • 20
  • 76
  • 116

1 Answers1

0

You can try an take a look at the answer I added for this similar question: https://stackoverflow.com/a/9003674/472747

Here is the code:

byte[] data = {10,10,10,10,10}; // better get this from a file or memory
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("server url");
ByteArrayBody bab = new ByteArrayBody(data, "image.jpg");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("image", bab);

FormBodyPart bodyPart=new FormBodyPart("formVariableName", new StringBody("formValiableValue"));
reqEntity.addPart(bodyPart);
bodyPart=new FormBodyPart("formVariableName2", new StringBody("formValiableValue2"));
reqEntity.addPart(bodyPart);
bodyPart=new FormBodyPart("formVariableName3", new StringBody("formValiableValue3"));
reqEntity.addPart(bodyPart); 
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = null;
while((line = in.readLine()) != null) {
    System.out.println(line);
}
Community
  • 1
  • 1
Alexandru Mincu
  • 780
  • 5
  • 6