views:

76

answers:

1

How can I call a Webservice with a byte[] parameter? Tried using the namedvaluepairs, but they take strings only. I am not using the Ksoap2, we decided to use the HttpClient / HttpPost method.

Anyone have any ideas?

Here is my code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    // WORKS WITH JPEG FILE -- String existingFileName = "/sdcard/dcim/Camera/1225231027592.jpg";
    String existingFileName = "/sdcard/dcim/0217175.jpg";
    String mMyFilename = "0217175.jpg";

    //  /sdcard/Music/kryptonite.mp3"; //DOES NOT WORK WITH MP3 FILE

    File uploadFile = new File(existingFileName);
    FileInputStream fileInputStream = null;
    try {
        fileInputStream = new FileInputStream(uploadFile);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";

    String mTest;
    String Method = null;

    try
    {

        //********************************************************************
        //  Create the HttpClient and the HttpPost
        //      Note:  Need to use HttpPost because of arguments/parameters
        //********************************************************************
        HttpClient client = new DefaultHttpClient();
        HttpPost request = new HttpPost();


        fileInputStream = new FileInputStream(uploadFile);

        // 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);

        //*************************************************************
        //Add Value Pairs for Parameters to pass to the Webservice
        //*************************************************************
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 



        //nameValuePairs.add(new BasicNameValuePair(buffer)); 

        nameValuePairs.add(new BasicNameValuePair("filename", mMyFilename)); 






    }
    catch (MalformedURLException ex) {
        // Log.e(Tag, "error: " + ex.getMessage(), ex);
    }
    catch (IOException ioe) {
        String s = "";

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

}

The Web Service:

[WebMethod]
public void PutFile(byte[] buffer, string filename)
{
    string serverpath;

    //serverpath = @"\\63.237.52.201\UploadFiles\Incoming\";
    serverpath = @"C:\Temp\";

    //BinaryWriter binWriter = new BinaryWriter(File.Open(Server.MapPath(filename), FileMode.CreateNew, FileAccess.ReadWrite));
    BinaryWriter binWriter = new BinaryWriter(File.Open(serverpath + filename, FileMode.CreateNew, FileAccess.ReadWrite));
    binWriter.Write(buffer);
    binWriter.Close();
}
A: 

You could convert it to a base64 representation and send the byte[] as an string. Then recover it at the server side. Just in case you don't find a better approach.

Guido
Ok - I am trying the base64 to the webservice and the file gets created, but when you try to open the jpg file there is nothing.
Tom
Code: String Base64EncodedData = Base64.encodeToString(buffer, 0) List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("base64data",Base64EncodedData)); nameValuePairs.add(new BasicNameValuePair("filename", mMyFilename));
Tom
Webservice:byte[] buffer = Convert.FromBase64String(base64data);File.WriteAllBytes(fullfilename_and_path, buffer);
Tom
Is there anything I am doing wrong? On android code or Webservice?
Tom
I can't say. Seems ok to me. These two questions are closely related: http://stackoverflow.com/questions/1067655/how-to-upload-a-file-using-java-httpclient-library-working-with-php-strange-pro and http://stackoverflow.com/questions/2105364/android-i-want-to-show-file-upload-progress-to-the-user
Guido