views:

34

answers:

0

I want to upload an image in blackberry simulator using MultipartPostData, the following is my code but it does not seem to work. I have also signed my .cod file. Can anyone help me please?

public void postData(String Url, bytes[] data)
{
 if (DeviceInfo.isSimulator()){
 Url=Url+";deviceSide=true";
}
HttpConnection httpConn=null;
OutputStream os=null;
InputStream is=null;
String url=Url;
try {
   PostData form = new MultipartPostData(MultipartPostData.DEFAULT_CHARSET, false) ;
   byte [] postData = data;
form.setData(postData);

      httpConn = (HttpConnection) Connector.open(url);
      httpConn.setRequestMethod(HttpConnection.POST);
   httpConn.setRequestProperty("User-Agent", "BlackBerry");
   httpConn.setRequestProperty("Content-Type", "multipart/form-data");
   httpConn.setRequestProperty("MIME-Type", "Image/Jpeg");
      httpConn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, String.valueOf(postData.length));
      httpConn.setRequestProperty("Content-Language", "en-US");

      os =httpConn.openOutputStream();
      os.write(form.getBytes());

    //read response
    StringBuffer sb = new StringBuffer();
    is = httpConn.openDataInputStream();
    int chr;
    while ((chr = is.read()) != -1)
      sb.append((char) chr);

    System.out.println("Result................................ " + sb.toString());
    String result=sb.toString();
}
catch(Exception e)
{
    System.out.println(e.toString());
}
finally {
    try{
        if(is!= null)
          is.close();
        if(os != null)
          os.close();
if(httpConn != null)
 httpConn.close();
 } catch(Exception e1){
        System.out.println(e1.toString());
    }
   }
 }