• Resolved crea8xion

    (@eyouthace)


    Hi Authors,

    Again, can you shed me a little light about media attachment. I had a code using HttpConnection to post an image from an android gallery/camera. I had a AsycnTask to upload an image as media to my server.

    wp-json/v2/media

    I had a successful connectinon to server with oauth-token but the problem is the server response if internal server error 500

    The image will be uploaded onActivityResult calling the AsycnTask with this code in doInBackground :

    connection = (HttpURLConnection) url.openConnection();
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);
    connection.setRequestMethod(“POST”);
    connection.setRequestProperty(“Connection”, “Keep-Alive”);
    connection.setRequestProperty(“User-Agent”, “Android Multipart HTTP Client 1.0”);
    connection.setRequestProperty(“Content-Type”, “multipart/form-data; boundary=” + boundary);

    outputStream = new DataOutputStream(connection.getOutputStream());
    outputStream.writeBytes(twoHyphens + boundary + lineEnd);
    outputStream.writeBytes(“Content-Disposition: form-data; name=\”” + filefield + “\”; filename=\”” + q[idx] + “\”” + lineEnd);
    outputStream.writeBytes(“Content-Type: ” + fileMimeType + lineEnd);
    outputStream.writeBytes(“Content-Transfer-Encoding: binary” + lineEnd);
    outputStream.writeBytes(lineEnd);
    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    buffer = new byte[bufferSize];
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    while (bytesRead > 0) {
    outputStream.write(buffer, 0, bufferSize);
    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    }
    outputStream.writeBytes(lineEnd);

    inputStream = connection.getInputStream();
    result = connection.getResponseCode();
    fileInputStream.close();
    inputStream.close();
    outputStream.flush();
    outputStream.close();

    https://www.ads-software.com/plugins/rest-api/

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Enlighten me for media attachment’ is closed to new replies.