Skip to content Skip to sidebar Skip to footer

Upload A File From Android To The Server Via Php

Can anyone help me to make my code work, i.e. upload a file from Android to the server via PHP? I tried it in many different ways but it won't work. I get HTTP Response 200 but the

Solution 1:

publicclassHelpherextendsAsyncTask<String, Void, String> {
    Context context;
    JSONObject json;
    ProgressDialog dialog;
    intserverResponseCode=0;
    DataOutputStreamdos=null;
    FileInputStreamfis=null;
    BufferedReaderbr=null;


    publicHelpher(Context context) {
        this.context = context;
    }

    protectedvoidonPreExecute() {

        dialog = ProgressDialog.show(Main2Activity.this, "ProgressDialog", "Wait!");
    }

    @Overrideprotected String doInBackground(String... arg0) {

        try {
            Filef=newFile(arg0[0]);
            URLurl=newURL("http://localhost:8888/imageupload.php");
            int bytesRead;
            HttpURLConnectionconn= (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);

            StringcontentDisposition="Content-Disposition: form-data; name=\"keyValueForFile\"; filename=\""
                    + f.getName() + "\"";
            StringcontentType="Content-Type: application/octet-stream";


            dos = newDataOutputStream(conn.getOutputStream());
            fis = newFileInputStream(f);


            dos.writeBytes(SPACER + BOUNDARY + NEW_LINE);
            dos.writeBytes(contentDisposition + NEW_LINE);
            dos.writeBytes(contentType + NEW_LINE);
            dos.writeBytes(NEW_LINE);
            byte[] buffer = newbyte[MAX_BUFFER_SIZE];
            while ((bytesRead = fis.read(buffer)) != -1) {
                dos.write(buffer, 0, bytesRead);
            }
            dos.writeBytes(NEW_LINE);
            dos.writeBytes(SPACER + BOUNDARY + SPACER);
            dos.flush();

            intresponseCode= conn.getResponseCode();
            if (responseCode != 200) {
                Log.w(TAG,
                        responseCode + " Error: " + conn.getResponseMessage());
                returnnull;
            }

            br = newBufferedReader(
                    newInputStreamReader(conn.getInputStream()));
            StringBuildersb=newStringBuilder();
            Stringline=null;
            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
            Log.d(TAG, "Sucessfully uploaded " + f.getName());

        } catch (MalformedURLException e) {
        } catch (IOException e) {
        } finally {
            try {
                dos.close();
                if (fis != null)
                    fis.close();
                if (br != null)
                    br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return String.valueOf(serverResponseCode);
    }


    @OverrideprotectedvoidonPostExecute(String result) {
        dialog.dismiss();

    }

}

This is the AsyncTask "Helpher" class used for upload image from Android. To call this class use like syntax below.

new Main2Activity.Helpher(this).execute(fileUri.getPath());

Here fileUri.getPath() local image location.

Post a Comment for "Upload A File From Android To The Server Via Php"