views:

188

answers:

2

Hi Everyone,

I'm trying to send an image file to a web service.

here's my code:

   ConnectionFactory connFact = new ConnectionFactory();
        ConnectionDescriptor connDesc;

        //String value = new String(data);
        String value= Base64OutputStream.encodeAsString(data, 0, data.length, false, false);

        value = "imgData=" + value;

        connDesc = connFact.getConnection("http://localhost:50806/Send");
        int iResponseCode = 0;
        HttpConnection httpConn = null;
        OutputStream os = null;
        if (connDesc != null)
        {

            try
            {
             byte[] theByteArray = value.getBytes();

                httpConn = (HttpConnection)connDesc.getConnection();

                httpConn.setRequestProperty("Content-Type", "image/jpeg");
                httpConn.setRequestMethod(HttpConnection.POST);
                httpConn.setRequestProperty("Content-Disposition", "form-data");
                httpConn.setRequestProperty("Connection", "Keep-Alive");
                httpConn.setRequestProperty("Content-Length", String.valueOf(data.length));

                //os = httpConn.openOutputStream();
                //os.write(theByteArray);
                DataOutputStream printout = new DataOutputStream (httpConn.openOutputStream()); 
                printout.write(theByteArray);
                iResponseCode = httpConn.getResponseCode();        
             } 
            catch(Exception e){
                e.printStackTrace();
            }
            finally{
                if(os != null)
                    os.close();

                if(httpConn != null){                    
                 httpConn.close();
                }
            }

        }
  return Integer.toString(iResponseCode);  

A byte array represented the image file is passed into the method.

service signature:

   [AcceptVerbs(HttpVerbs.Post)]  
    public ActionResult Send(string imgData)
    {}

Any ideas on how to send this?

Thanks so much, Dan

+1  A: 

A Base64-encoded string is probably going to be too large to pass as a URL parameter in a GET request. Either the device or some proxy along the way may do something bad to the request. It would be much safer using a POST request. To do this, set the request as a POST with httpConn.setRequestMethod(HttpConnection.POST) and then write all of the form parameters into the body of the request using httpConn.openOutputStream() (and possibly the PostData class)

Marc Novakowski
Hey Marc, Thanks for the help. I've updated the code(see above). Does that look right to you? What would my web service's method signature look like? I'm not sure how to get the byte data. Thanks again.
Dan
The code looks good - you probably don't need to flush the OS since the call to httpConn.getResponseCode() will automatically flush it. As for the webservice, can't really help there as it depends on the server platform, etc. Basically though you just want to be able to read the bytes directly from the body - relatively easy to do in Java servlets, PHP, etc.
Marc Novakowski
Hey Marc, thanks for the reply. I'm using asp.net mvc. I can get everything to work when I'm sending smaller string values to the controller. but I can seem to get the post value. I've updated the code and included my controller's method signature. Let me know if anything doesn't look right :) Thanks again, very much.
Dan
You should probably create a new Stackoverflow question for your server-side problem and tag it with "asp.net", etc. since this question has veered from the original BlackBerry client problem
Marc Novakowski
Okay. that's a good idea. Thanks a lot for your help! Take care.
Dan
A: 
  1. Netbeans 6.9
  2. Create a project Helloworld
  3. Deploy and test the service
  4. Right Click on project properties set relative url as resources/helloworld

/* * To change this template, choose Tools | Templates * and open the template in the editor. */

package helloworld;

import javax.ws.rs.core.Context; import javax.ws.rs.core.UriInfo; import javax.ws.rs.PathParam; import javax.ws.rs.Consumes; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Produces; import javax.ws.rs.core.Response; import javax.xml.bind.JAXBElement; import javax.ws.rs.FormParam; import javax.ws.rs.QueryParam; import java.util.ArrayList; import java.util.List;

/** * REST Web Service * * @author localadmin */

@Path("helloworld") public class HelloWorld { @Context private UriInfo context;

/** Creates a new instance of HelloWorld */
public HelloWorld() {
}

/**
 * Retrieves representation of an instance of helloworld.HelloWorld
 * @return an instance of java.lang.String
 */
@POST

// @Produces("text/html") @Produces("application/xml") // @Consumes("application/x-www-form-urlencoded") // public List getHtml(@QueryParam("name") String user_id) { public List getHtml(@FormParam("test") String user_id) { userData toReturn = new userData(); toReturn.name = user_id; List retUser = new ArrayList(); retUser.add(toReturn); return retUser; }

/**
 * PUT method for updating or creating an instance of HelloWorld
 * @param content representation for the resource
 * @return an HTTP respons with content of the updated or created resource.
 */
@PUT
@Consumes("text/html")
public void putHtml(String content) {
}

}

2nd Class

/* * To change this template, choose Tools | Templates * and open the template in the editor. */

package helloworld;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement public class userData { public String name;

}

And the post html

Reply

Forward

Reply | Harshal Shah

show details 8:11 PM (1 hour ago)

<script type="text/javascript">
  function register(){
//    window.open("/welcome.html",'welcome','width=300,height=200,menubar=yes,status=yes,location=yes,toolbar=yes,scrollbars=yes');
    alert("hey");
    $.ajax({
        type: "GET",
        url: "http://localhost:8080/HelloWorld/resources/helloworld",
        data: "user_id=" + document.getElementById("user_id").value ,
        success: function(msg){
                        window.alert(msg);
                    },
            error: function(xmlHttpRequest, status, err) {
                alert('Status ' + status + ', Error ' + err);
            }
});
    }
</script>

hello

harshal