0

I need to upload my file android device to server using webservice. But unfortunately Im getting error while doing it.Please help me to clear this error.

Here is my java code:

         public class HttpFileUpload implements Runnable {
         URL connectURL;
        String responseString;
         String Title;
String Description;
byte[ ] dataToServer;
FileInputStream fileInputStream = null;

HttpFileUpload(String urlString, String vTitle, String vDesc){
    try{
        connectURL = new URL(urlString);
        Title= vTitle;
        Description = vDesc;
    }catch(Exception ex){
        Log.i("HttpFileUpload","URL Malformatted");
    }
}

void Send_Now(FileInputStream fStream){
    fileInputStream = fStream;
    Sending();
}

void Sending(){
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    String iFileName = "ovicam_temp_vid.mp4";
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";
    String Tag="fSnd";
    try
    {
        Log.e(Tag,"Starting Http File Sending to URL");

        // Open a HTTP connection to the URL
        HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();

        // Allow Inputs
        conn.setDoInput(true);

        // Allow Outputs
        conn.setDoOutput(true);

        // Don't use a cached copy.
        conn.setUseCaches(false);

        // Use a post method.
        conn.setRequestMethod("POST");

        conn.setRequestProperty("Connection", "Keep-Alive");

        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

        DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"title\""+ lineEnd);
        dos.writeBytes(lineEnd);
        dos.writeBytes(Title);
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + lineEnd);

        dos.writeBytes("Content-Disposition: form-data; name=\"description\""+ lineEnd);
        dos.writeBytes(lineEnd);
        dos.writeBytes(Description);
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + lineEnd);

        dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + iFileName +"\"" + lineEnd);
        dos.writeBytes(lineEnd);

        Log.e(Tag,"Headers are written");

        // create a buffer of maximum size
        int bytesAvailable = fileInputStream.available();

        int maxBufferSize = 1024;
        int bufferSize = Math.min(bytesAvailable, maxBufferSize);
        byte[ ] buffer = new byte[bufferSize];

        // read file and write it into form...
        int bytesRead = fileInputStream.read(buffer, 0, bufferSize);

        while (bytesRead > 0)
        {
            dos.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable,maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0,bufferSize);
        }
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        // close streams
        fileInputStream.close();

        dos.flush();

        Log.e(Tag,"File Sent, Response: "+String.valueOf(conn.getResponseCode()));

        InputStream is = conn.getInputStream();

        // retrieve the response from server
        int ch;

        StringBuffer b =new StringBuffer();
        while( ( ch = is.read() ) != -1 ){ b.append( (char)ch ); }
        String s=b.toString();
        Log.i("Response",s);
        dos.close();
    }
    catch (MalformedURLException ex)
    {
        Log.e(Tag, "URL error: " + ex.getMessage(), ex);
    }

    catch (IOException ioe)
    {

        ioe.printStackTrace();
        Log.e(Tag, "IO error: " + ioe.getMessage(), ioe);
    }
}

@Override
public void run() {
    // TODO Auto-generated method stub
}

}

LogCat:

java.io.FileNotFoundException: http:///****8*******8/*****
            at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:210)
            at arun.com.test.HttpFileUpload.Sending(HttpFileUpload.java:118)
            at arun.com.test.HttpFileUpload.Send_Now(HttpFileUpload.java:36)
Bhargav
  • 8,118
  • 6
  • 40
  • 63
Vignesh
  • 21
  • 4
  • This means a http 404 error occured, is your website reachable from the mobile browser? – Bhargav Feb 24 '16 at 07:59
  • Refer this http://stackoverflow.com/a/2274535/4128945 and this http://stackoverflow.com/q/5379247/4128945 – Bhargav Feb 24 '16 at 08:04
  • @Vignesh One of the reason you can get this error is your functions return type, if it is void type then you can get this error on `getIputStream()` so make sure you function return you some thing – Jaiprakash Soni Feb 24 '16 at 09:44

1 Answers1

0

the average REST webservice won't return a response body on POST/PUT, just a response status. You'd like to determine it instead of the body.

Replace

InputStream response = conn.getInputStream();

by

int status = conn.getResponseCode();

All available status codes and their meaning are available in the HTTP spec, as linked before. The webservice itself should also come along with some documentation which overviews all status codes supported by the webservice and their special meaning, if any.

If the status starts with 4nn or 5nn, you'd like to use getErrorStream() instead to read the response body which may contain the error details.

InputStream error = con.getErrorStream();

Taken from this answer

Community
  • 1
  • 1
Bhargav
  • 8,118
  • 6
  • 40
  • 63