BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 10-01-2010, 04:38 AM   #1 (permalink)
New Member
 
Join Date: Oct 2010
Model: 9500
PIN: N/A
Carrier: Airtel
Posts: 1
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem posting data to WCF/Rest from blackberry application

Please Login to Remove!

Hello,

I am trying to Post data from my Blackberry application through HttpConnection to WFC/REST service. The data is in XML format.
Can someone please give me a solution to post data to WCF ? Are there any specific configuration settings that need to be set for BlackBerry?

It is working perfectly when Iím running from J2ME WTK emulator but the same application when I run on Blackberry Emulator, the server throwing an error.

[ The server encountered an error processing the request. The exception message is 'The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.'. See server logs for more details. The exception stack trace is:</P>
<P class="intro"> at System.ServiceModel.Dispatcher.DemultiplexingDispa tchMessageFormatter.DeserializeRequest(Message message, Object[] parameters)
at System.ServiceModel.Dispatcher.UriTemplateDispatch Formatter.DeserializeRequest(Message message, Object[] parameters)
at System.ServiceModel.Dispatcher.CompositeDispatchFo rmatter.DeserializeRequest(Message message, Object[] parameters)
at System.ServiceModel.Dispatcher.DispatchOperationRu ntime.DeserializeInputs(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.DispatchOperationRu ntime.InvokeBegin(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRu ntime.ProcessMessage5(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRu ntime.ProcessMessage4(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRu ntime.ProcessMessage3(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRu ntime.ProcessMessage2(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.ImmutableDispatchRu ntime.ProcessMessage1(MessageRpc& rpc)
at System.ServiceModel.Dispatcher.MessageRpc.Process( Boolean isOperationContextSet)
]

Here is my code for HttpConnection :

Code:
private void configureConncetion(HttpConnection conn) throws IOException {

		//conn.setRequestProperty("User-Agent", ua);
		String locale = System.getProperty("microedition.locale");
		if (locale == null) { locale = "en-US";	}
		conn.setRequestProperty("Accept-Language", locale);
		conn.setRequestProperty("Content-Type", "text /xml");
		
	}

public HttpConnection getConnection(String url, int access) throws IOException {
		HttpConnection conn = (HttpConnection) Connector.open(url, access);
		configureConncetion(conn);
		return conn;
	}

public String post(String url, String data) throws IOException {

		HttpConnection hcon = null;
		DataInputStream dis = null;
		DataOutputStream dos = null;
		StringBuffer responseMessage = new StringBuffer();
		try {
			int redirectTimes = 0;
			boolean redirect;
			do {
				redirect = false;
				// an HttpConnection with both read and write access
				hcon = getConnection(url, Connector.READ_WRITE);
				// set the request method to POST
				hcon.setRequestMethod(HttpConnection.POST);
				// overwrite content type to be form based
				//hcon.setRequestProperty("Content-Type", "text/xml");
				// set message length
				if (data != null) {
					hcon.setRequestProperty("Content-Length", ""
							+ data.length());
				}

				if (data != null) {
					// obtain DataOutputStream for sending the request string
					dos = hcon.openDataOutputStream();
					byte[] request_body = data.getBytes();
					// send request string to server
					for (int i = 0; i < request_body.length; i++) {
						dos.writeByte(request_body[i]);
					}// end for( int i = 0; i < request_body.length; i++ )
					dos.flush(); // Including this line may produce
					// undesiredresults on certain devices
				}

				// obtain DataInputStream for receiving server response
				dis = new DataInputStream(hcon.openInputStream());
				// retrieve the response from server
				int ch;
				while ((ch = dis.read()) != -1) {
					responseMessage.append((char) ch);
				}// end while( ( ch = dis.read() ) != -1 ) {
				// check status code
				int status = hcon.getResponseCode();
                                		switch (status) {
				case HttpConnection.HTTP_OK: // Success!
					break;
				case HttpConnection.HTTP_TEMP_REDIRECT:
				case HttpConnection.HTTP_MOVED_TEMP:
				case HttpConnection.HTTP_MOVED_PERM:
					// Redirect: get the new location
					url = hcon.getHeaderField("location");
					System.out.println("Redirect: " + url);

					if (dis != null) dis.close();
					if (hcon != null) hcon.close();
					hcon = null;
					redirectTimes++;
					redirect = true;
					break;
				default:
					// Error: throw exception
					hcon.close();
					throw new IOException("Response status not OK:" + status);
				}

				// max 5 redirects
			} while (redirect == true && redirectTimes < 5);

			if (redirectTimes == 5) {
				throw new IOException("Too much redirects");
			}
		} catch (Exception e) {
			e.printStackTrace();
			responseMessage.append("ERROR");
                          VLogFile.writeLog("Entered REST Exception"+e.getMessage());
		} finally {
			// free up i/o streams and http connection
			try {
				if (hcon != null) hcon.close();
				if (dis != null) dis.close();
				if (dos != null) dos.close();
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}// end try/catch
		}// end try/catch/finally
		return responseMessage.toString();
	}
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright © 2004-2014 BlackBerryForums.com.
The names RIM © and BlackBerry © are registered Trademarks of BlackBerry Inc.