I have a question about httpConnection and getResponseCode on BlackBerry - blackberry

if I try to connect jpeg Camera, this connection works right. But When I connect to a Mjpeg (jpeg-stream) Camera, I can't show "System.out.println("onreturn oncesi"
+ httpConnection.getResponseCode());" on Output Console.
I am using Emulator and MDS. I can show on MDS, ... stream is coming.
url = getUrl();
queryString = encodeURL(queryString);
byte postmsg[] = queryString.getBytes("UTF-8");
httpConnection = (HttpConnection) Connector.open(url
+ ";deviceside=false", Connector.READ_WRITE);
httpConnection.setRequestMethod(HttpConnection.GET);
httpConnection.setRequestProperty("Authorization", getBase64Encode());
os = httpConnection.openDataOutputStream();
for (int i = 0; i < postmsg.length; i++) {
os.write(postmsg[i]);
}
{
if (!cancel) {
System.out.println(httpConnection.getURL()+
" *****"+httpConnection.getPort());
System.out.println("onreturn oncesi"
+ httpConnection.getResponseCode());
onReturn(httpConnection.getResponseCode(), httpConnection
.openInputStream(),(int) httpConnection.getLength());
System.out.println("onreturn sornrası");
}
os.close();
httpConnection.close();
}
} catch (Exception e) {
System.out.println("hata " + e.getMessage());
try {
httpConnection.close();
Thread.sleep(60);
} catch (Exception ie) {
}
onError(e);
}

One problem is your not setting your request headers correctly.
Your Posting data, so shouldn't httpConnection.setRequestMethod(HttpConnection.GET); be httpConnection.setRequestMethod(HttpConnection.POST);.
And you should also set the content-Length: httpConnection.setRequestProperty("Content-Length", Integer.toString(postmsg.length));
And while we are at it I would go ahead and set these:
content type: maybe as "image/jpeg". Not sure what it should be for mJpeg... httpConnection.setRequestProperty("Content-Type", "image/jpeg");
UserAgent I have found that some sites block the default user-agent in RIM (Java/xxx), thinking it's a spider, so I like to set the user agent.httpConnection.setRequestProperty("User-Agent", "MyCoolApp/V1 (App_RIM)");
What version of the JDE are you using? Is this over HTTPS? I ask because on older versions like 4.5 you had to create http and https differently. In the newer versions you should probably use the new ConnectionFactory, instead of Connector.
Good Luck and I hope you figure it out!!!

Related

HTTP Error 403: Forbidden , BlackBerry Browser

I am running a local server with xampp. I have some html files on it. I can run the files from my computer browser and from my iphone , my android phone and windows phone.
However when i try the BlackBerry i get the message :
HTTP Error 403: Forbidden . You are not authorized to view this page. Please try loading a different page.
If i press to view Details :
The following error was encountered while trying to retrieve the URL : myServersUrl
Access Denied
Access control configuration prevents your request from being allowed at this time. Please contact your service provider if you feel this is incorrect.
Now i was very careful to connect to the same wifi , as my computer that runs the server is connected , i type the ip correctly as i do to my other phones that i connect with no problem and i have my 3g closed so that i know it will use the wifi. Also i can open any page from the browser, that means i do have internet on the phone. BIS/BES are enabled.
Any idea why i cant connect on my local server here?
Some Code
I try to connect like this :
BrowserFieldConfig myBrowserFieldConfig = new BrowserFieldConfig();
myBrowserFieldConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE,BrowserFieldConfig.NAVIGATION_MODE_POINTER);
BrowserField browserField = new BrowserField(myBrowserFieldConfig);
add(browserField);
//attaching the udid on the URL
browserField.requestContent("http://192.123.5.112/Server_CityInfo/jMobile.html?" + udid);
and
public static HttpConnection getHttpConnection(String url, byte[] postData) {
HttpConnection conn = null;
OutputStream out = null;
try {
conn = (HttpConnection) new ConnectionFactory().getConnection(url).getConnection();
if (conn != null) {
if (postData == null) {
conn.setRequestMethod(HttpConnection.GET);
conn.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
} else {
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty("Content-Length", String.valueOf(postData.length));
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
out = conn.openOutputStream();
out.write(postData);
out.flush();
}
if (conn.getResponseCode() != 0) {
return conn;
}
}
} catch (Exception e) {
} finally {
try {
out.close();
} catch (Exception e2) {
}
}
//Only if exception occurs, we close the connection.
//Otherwise the caller should close the connection himself.
try {
conn.close();
} catch (Exception e1) {
}
return null;
}
It would help if you included the relevant code from the BlackBerry app that sets up the connection. If you're not using the ConnectionFactory, you have to add appenders to make sure the connection is routed to the transport you are expecting. This is why including the code is helpful.

How to compress the files in Blackberry?

In my application I used html template and images for browser field and saved in the sdcard . Now I want to compress that html,image files and send to the PHP server. How can I compress that files and send to server? Provide me some samples that may help lot.
i tried this way... my code is
EDIT:
private void zipthefile() {
String out_path = "file:///SDCard/" + "newtemplate.zip";
String in_path = "file:///SDCard/" + "newtemplate.html";
InputStream inputStream = null;
GZIPOutputStream os = null;
try {
FileConnection fileConnection = (FileConnection) Connector
.open(in_path);//read the file from path
if (fileConnection.exists()) {
inputStream = fileConnection.openInputStream();
}
byte[] buffer = new byte[1024];
FileConnection path = (FileConnection) Connector
.open(out_path,
Connector.READ_WRITE);//create the out put file path
if (!path.exists()) {
path.create();
}
os = new GZIPOutputStream(path.openOutputStream());// for create the gzip file
int c;
while ((c = inputStream.read()) != -1) {
os.write(c);
}
} catch (Exception e) {
Dialog.alert("" + e.toString());
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
Dialog.alert("" + e.toString());
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
Dialog.alert("" + e.toString());
}
}
}
}
this code working fine for single file but i want to compress all the file(more the one file)in the folder .
In case you are not familiar with them, I can tell you that in Java the stream classes follow the Decorator Pattern. These are meant to be piped to other streams to perform additional tasks. For instance, a FileOutputStream allows you to write bytes to a file, if you decorate it with a BufferedOutputStream then you get also buffering (big chunks of data are stored in RAM before being finally written to disc). Or if you decorate it with a GZIPOutputStream then you get also compression.
Example:
//To read compressed file:
InputStream is = new GZIPInputStream(new FileInputStream("full_compressed_file_path_here"));
//To write to a compressed file:
OutputStream os = new GZIPOutputStream(new FileOutputStream("full_compressed_file_path_here"));
This is a good tutorial covering basic I/O . Despite being written for JavaSE, you'll find it useful since most things work the same in BlackBerry.
In the API you have these classes available:
GZIPInputStream
GZIPOutputStream
ZLibInputStream
ZLibOutputStream
If you need to convert between streams and byte array use IOUtilities class or ByteArrayOutputStream and ByteArrayInputStream.

Blackberry Push Client Application Blackberry Registration failed

I am quite new to blackberry application development and i need to create an application which can receive push notifications.
I have created the application and was trying to use xtify for pushing notifications to devices.
I have registered with rim for the push evaluation and got the credentials like url,app id,password etc.
When the application launches , i create a new thread to perform the push registration process.
I try to send an http Get request to the push evaluation url for registering the device.When i try to open a connection,i get the io exception,invalid url parameter.
I am using a wifi connection for network connectivity in device. I donot have a sim in the device.
The url is
http://cpXXX.pushapi.eval.blackberry.com/mss/PD_subReg?serviceid='My Application Id'&osversion='My OS Version'&model='Device Model';deviceside=false;ConnectionType=mds-public
Below is the code i use to send the request.
DataBuffer buffer = new DataBuffer(256, false);
httpUrl = "http://cpXXX.pushapi.eval.blackberry.com/mss/PD_subReg?serviceid='My Application Id'&osversion='My OS Version'&model='Device Model';deviceside=false;ConnectionType=mds-public"
InputStream inputStream = null;
Connection conn = null;
HttpConnection httpConn = null;
try {
httpUrl ;
conn = Connector.open(httpUrl);
if (conn instanceof HttpConnection) {
httpConn = (HttpConnection) conn;
int responseCode = httpConn.getResponseCode();
if(responseCode == 200){
inputStream = httpConn.openInputStream();
int length = inputStream.read(buffer.getArray());
buffer.setLength(length);
String response = new String( buffer.getArray(), buffer.getArrayStart(), buffer.getArrayLength() );
return response;
}else {
throw new IOException( "Http error: " + responseCode);
}
}
else {
throw new IOException("Can not make HTTP connection for URL '"
+ httpUrl + "'");
}
}
finally {
if (httpConn != null) {
try {
httpConn.close();
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
close(conn, inputStream, null);
}
Please help me. I am waiting for replies. I have been stuck with this for days. Please advice. Anyone have idea where i can get a documentation for the device registration api ??
Thanx to the advice of #Black Pearl , I was able to resolve the issue. Inorder for the blackberry registration to work , you need an active BIS connection. I had been trying it with wifi connectivity and hence it was not working.

None of code can establish http connection over BIS

I am new in developing Blackberry Application.
In these three days, I already searched and learned in both forum and tutorial from the RIM itself. But none of them can solve my problem. >.<
So. I already tried some different methods to establish http connection over BIS in 4.6.
These are the following codes:
1.
HttpConnection httpConnection;
String url = "myURL;deviceside=true";
try{
httpConnection = (HttpConnection) Connector.open(url);
Dialog.inform(">.<");
}
catch(Exception e)
{
Dialog.inform(e.getMessage());
}
From the code #1 above, none of the dialogs are displayed.
String url = "myURL";
try {
StreamConnection s = (StreamConnection)Connector.open(url);
InputStream input = s.openInputStream();
Dialog.inform("sblm byte");
byte[] data = new byte[256];
int len = 0;
StringBuffer raw = new StringBuffer();
Dialog.inform("stlh buat byte");
while( -1 != (len = input.read(data))) {
raw.append(new String(data, 0, len));
}
Dialog.inform("stlh while");
response = raw.toString();
Dialog.inform(response);
input.close();
s.close();
}
catch(Exception e) { }
As well as code #1, this code above also doesnt pop up any dialog.
I am desperately need the right guide for establishing simple http connection. Is there any technique that I missed? Do I need any signature for this? Do I need extra setting in both my Blackberry device (BB 8900 with OS 5.00) or in my compiler, Eclipse?
Thank you.
Try this code.
try {
HttpConnection httpConnection=(HttpConnection)Connector.open(url);
httpConnection.setRequestMethod(HttpConnection.GET);
if(httpConnection.getResponseCode()==HttpConnection.HTTP_OK)
{
InputStream is=httpConnection.openInputStream();
int ch;
StringBuffer buffer=new StringBuffer();
while((ch=is.read())!=-1)
{
buffer.append((char)ch);
}
}
} catch (IOException e) {
System.out.println("Exception From Thread"+e);
e.printStackTrace();
}
}

Problems opening http connection from blackberry simulator

I'm having trouble when opening a simple HttpConnection from the simulator, I've have appended the deviceside=true suffix to my url, however it's still not working, I'm receiving an empty httpconnection with response code of 0. This is the code that's giving me problems:
public void readUrl(){
HttpConnection conn=null;
try {
conn = (HttpConnection) Connector.open("http://www.google.com;deviceside=true");
conn.setRequestMethod("GET");
if(conn.getResponseCode()==HttpConnection.HTTP_OK){
System.out.println("Create connection sucessfully");
}
} catch (ConnectionNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
DataInputStream din=null;
ByteVector responseBytes=null;
try {
din = conn.openDataInputStream();
responseBytes = new ByteVector();
int i = din.read();
while (-1 != i) {
responseBytes.addElement((byte) i);
i = din.read();
}
} catch (IOException e) {
//TODO: HANDLE EXCEPTIONS
e.printStackTrace();
}
responseBytes.toArray();
I have no idea what's going on. It supposed that by appending the deviceside=true it should connect directly. Anyway I tried too installing the MDS server and setting my url to deviceside=false, but the result was the same.
Now I tested the same code using a local url like http://localhost:8080/resources/mypage.html, and It worked as expected, so I was wondering if this could be a simulator configuration issue. How can I solve it?
Thanks a lot.
In my experience, you need to append ;deviceside=true when using the MDS simulator. There's a great post on the blackberry.com forums that shows you how to determine what connection suffix you should be using, as well as some general good advice on using connections in BlackBerry.
For something to help make it easier to get the content of your request, you can use the IOUtilities class:
InputStream stream = conn.openInputStream();
String contents = new String(IOUtilities.streamToBytes(stream));
";deviceside=true" is for DIRECT TCP transport. To use MDS transport you need to append with ";deviceside=false".
When you run on the device simulator you can use DIRECT TCP transport without the need of starting the MDS simulator. However if you want to test MDS transport, then you need to start MDS simulator before you start the device simulator.
In the Simulator setup tabs "General" do you have the "Launch MDS-CS with simulator" checked?
If so, you do not need to append any suffix at all...
Yes you're right, with deviceside=true the internet connection was used, however it seemed like it was a problem whit the HttpConnection class, when I used this code instead:
public StreamConnection openConnection(){
StreamConnection conn=null;
try {
conn = (StreamConnection) Connector.open(url+";deviceside=true");
//conn.setRequestMethod(httpMethod);
} catch (ConnectionNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
return conn;
}
It worked correctly, so I was wondering something...when opening a connection in blackberry where I should put my code for checking the response code. After creating the connection? like the code above or after opening a dataStream like:
din = conn.openDataInputStream();
responseBytes = new ByteVector();
int i = din.read();
while (-1 != i) {
responseBytes.addElement((byte) i);
i = din.read();
}
Thanks.

Resources