Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Trusted Contributor
Posts: 242
Registered: ‎03-16-2010
My Device: 9530

Problem regarding HTTP POST

[ Edited ]

 

Hello,

 

    I have to pass 2 parameters (String and byte[]) to my web service using POST method. Initially had to send only byte[] of an image and this is my code :

 

   public void uploadImage() throws Exception {
        HttpConnection c = null;
        InputStream is = null;
        OutputStream os = null;
        int rc;
        String urlStr = url + "service=editimage";
        byte[] imageBytes;
        String path = "store/samples/folder icons/folder01.png";
        String boundary = "Unique_Text_Also_Alphanumeric";
        
        try {
            imageBytes = getFileBytes("store/samples/folder icons/folder01.png");
            System.out.println("******** Image Bytes of file = " + path + " Bytes Size = " + imageBytes.length);
        } catch (Exception e) {
            throw new IOException(e.getMessage());
        }
        
        try {
            c = (HttpConnection)Connector.open(urlStr, Connector.READ_WRITE);
            // Set the request method and headers
            c.setRequestMethod(HttpConnection.POST);
            //conn.setRequestProperty("Content-Length", ""+Bytes_Of_Image_To_Be_Uploaded.length);
            c.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_DATA+";boundary="+boundary);
            c.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, String.valueOf(imageBytes.length));
            c.setRequestProperty( "x-rim-transcode-content", "none" );

            System.out.println("******** Got connection & set request headers");
            
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            os = c.openOutputStream();
            
            String newLine = "\r\n";
            out.write(newLine.getBytes());
            out.write("--".getBytes());
            out.write(boundary.getBytes());
            out.write(newLine.getBytes());
            
            String contDisp = "Content-Disposition:form-data; name=\"Image.jpg\";filename=\"Image.jpg\"";
            String contEnc = "Content-Transfer-Encoding: binary";
            String type="Content-Type:image/jpeg";
            
            out.write(contDisp.getBytes());
            out.write(newLine.getBytes());
            out.write(type.getBytes());
            out.write(newLine.getBytes());
            out.write(contEnc.getBytes());
            out.write(newLine.getBytes());
            out.write(newLine.getBytes());
            
            System.out.println("******** Wrote before imageBytes");
            
            out.write(imageBytes);
            System.out.println("******** imageBytes writen");
            
            out.write(newLine.getBytes());
            out.write("--".getBytes());
            out.write(boundary.getBytes());
            out.write("--".getBytes());
            out.write(newLine.getBytes());
            System.out.println("******** other also written ");
            
            os.write(out.toByteArray());
            System.out.println("******** WROTE TO MAIN"); 
            out.flush();
            out.close();
            
            os.flush();
            os.close();
            System.out.println("SEND Now rcving Response"); 
            
            rc = c.getResponseCode();
            System.out.println("REsponse Code = " + rc);
            if (rc != HttpConnection.HTTP_OK) 
                throw new IOException("HTTP Response Code : " + rc);
                
            is = c.openInputStream();
            String restype = c.getType();
            int len = (int)c.getLength();
            System.out.println("Type = " + restype + " Len = " + len);
            
            if (len > 0) {
                int actual = 0;
                int bytesRead = 0;
                byte[] data = new byte[len];
                while(bytesRead != len && (actual != -1)) {
                    actual = is.read(data, bytesRead, len - bytesRead);
                    bytesRead += actual;
                }
                String s = new String(data);
                System.out.println("Bytes Rcvd String = " + s);
                
            } else {
                int ch;
                StringBuffer answer = new StringBuffer();
                while ((ch = is.read()) != -1) {
                    answer.append((char)ch);
                }
                System.out.println("READ reply = " + answer.toString());
            }
            System.out.println("***** FINISHED uploadImage");
        } catch(ClassCastException e) {
            throw new IllegalArgumentException("Not an HTTP URL");
        } catch(Exception e) {
            throw new IOException("Failed to Upload : " + e.getMessage());
        }
        
        return;
    }

 I can see SOP's till "******** WROTE TO MAIN", can't see after that. What can be the reason and fault ? I don't see any error or exception, but nothing comes next.

 

Now have to pass 2 parameters (String, byte[] for image) with its parameter name respectively.  How can I set parameter name and its value ?  

 

Can anyone help me out. I looked around net and this forum couldn't find more helpful answers. Any help is highly appreciated.

 

 

Thanks

 


--------------------------------------------------------------------------------
For any helpful answers give kudo's (star) to the helper.
Trusted Contributor
Posts: 242
Registered: ‎03-16-2010
My Device: 9530

Re: Problem regarding HTTP POST

 

I have to send 2 parameters 1 - strign and other the byte[]. How do I set parameters and send this.

 

The code is working till "******** WROTE TO MAIN", then it hangs off. I guess b'coz its not getting the parameter name. I am stuck and need help.

 

Any help is highly appreciated.

 

Thanks

 


--------------------------------------------------------------------------------
For any helpful answers give kudo's (star) to the helper.
Developer
Posts: 177
Registered: ‎01-21-2009
My Device: 9900
My Carrier: Vodafone

Re: Problem regarding HTTP POST

Attach a debugger and see where it blocks. I suspect the call to os.flush() is the culprit, and the reason is because you might be getting a tunnel error. On blackberry the connection path is specified by the developer via appended parameters to your url string. Read up on how to create an http connection on BlackBerry. There is a sticky thread by Peter Strange that outlines this process here:

 

http://supportforums.blackberry.com/t5/Java-Development/Sample-HTTP-Connection-code-and-BIS-B-Access...

Trusted Contributor
Posts: 242
Registered: ‎03-16-2010
My Device: 9530

Re: Problem regarding HTTP POST

 

Thanks pwerry,

 

    I already have that code with me and tested passing only string as parameter and that works well. But I got to append byte[] (image) also. I cannot append byte[] to URLEncodedPostData.

 

   The other way have already provided the code in 1st message, but don't know how to give name of parameter, it value and also add byte[] as parameter.

 

   By searching more also found a link http://supportforums.blackberry.com/t5/Java-Development/how-to-pass-parameters-into-http-post-method... where "trainee" has provided example that worked for him; but that also works only for J2ME and doesn't work on BB.

 

   Now how to achieve. I am sure theri must be some way which I am missing. Need to find that missing step only.

 

Thanks 

 


--------------------------------------------------------------------------------
For any helpful answers give kudo's (star) to the helper.