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
Highlighted
New Developer
Posts: 12
Registered: ‎07-30-2014
My Device: Blackberry Curve 9380
My Carrier: Etisalat

Unable to post data to Server

[ Edited ]

I'm using the below class to post some data to server but getting error. Can anyone tell where I'm going wrong.

 

public class HttpPost extends Thread implements Runnable {
	HttpConnection hc = null;
	StringBuffer stringBuffer = new StringBuffer();
	InputStream inputStream;

	public HttpPost(String url) {
		try {

			String connectionParameters = "";
			if (WLANInfo.getWLANState() == WLANInfo.WLAN_STATE_CONNECTED) {

				connectionParameters = ";interface=wifi";
			} else {
				int coverageStatus = CoverageInfo.getCoverageStatus();
				ServiceRecord record = getWAP2ServiceRecord();
				if (record != null
						&& (coverageStatus & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {

					connectionParameters = ";deviceside=true;ConnectionUID="
							+ record.getUid();
				} else if ((coverageStatus & CoverageInfo.COVERAGE_MDS) == CoverageInfo.COVERAGE_MDS) {

					connectionParameters = ";deviceside=false";
				} else if ((coverageStatus & CoverageInfo.COVERAGE_DIRECT) == CoverageInfo.COVERAGE_DIRECT) {

					connectionParameters = ";deviceside=true";
				}
			}

			hc = (HttpConnection) Connector.open(url + connectionParameters);
			URLEncodedPostData oPostData = new URLEncodedPostData(
					URLEncodedPostData.DEFAULT_CHARSET, false);

			
			oPostData.append("userName", "david");
			oPostData.append("userId", "1");
			oPostData.append("category","Student");
			oPostData.append("Address1", "16 Street");
			oPostData.append("Address2", "Chicago");


			hc.setRequestMethod(HttpConnection.POST);
			hc.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE,
					oPostData.getContentType());
			byte[] postBytes = oPostData.getBytes();
			hc.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH,
					Integer.toString(postBytes.length));
			OutputStream strmOut = hc.openOutputStream();
			strmOut.write(postBytes);
			strmOut.flush();
			strmOut.close();

			String returnMessage = hc.getResponseMessage();
			System.out.println(returnMessage);
			if (hc.getResponseCode() == HttpConnection.HTTP_OK) {
				inputStream = hc.openInputStream();
				int c;
				while ((c = inputStream.read()) != -1) {
					stringBuffer.append((char) c);
				}
				System.out.println(stringBuffer.toString());
				parseResults(stringBuffer.toString());

			} else {
				parseResults("ERROR");
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	private void parseResults(String response) {
		if (response.equalsIgnoreCase("ERROR")) {
			System.out
					.println("Error in Connection please check your internet and Connection extension");
		} else {
			System.out.println(response);
		}
	}

	private ServiceRecord getWAP2ServiceRecord() {
		ServiceBook sb = ServiceBook.getSB();
		ServiceRecord[] records = sb.getRecords();

		for (int i = 0; i < records.length; i++) {
			String cid = records[i].getCid().toLowerCase();
			String uid = records[i].getUid().toLowerCase();
			if (cid.indexOf("wptcp") != -1 && uid.indexOf("wifi") == -1
					&& uid.indexOf("mms") == -1) {
				return records[i];
			}
		}
		return null;
	}

}

 The above class is called as

 

HttpPost post = new HttpPost(http://www.myUrl.com/post.ashx);
post.start();

 

Developer
Posts: 16,987
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Unable to post data to Server

use ConnectionFactory.

If you get an error it would be helpful to write some details about that, the crystal balls are all out of energy.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter