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
Contributor
bb_eibl
Posts: 26
Registered: ‎04-09-2013
My Device: not specified

Re: httpConnection can´t send query, difficulties with setRequestProperty

[28.945] Protocol: http
[28.945] URL: http://192.168.0.110/Heizung.htm;deviceside=true
[28.96] VM:+CR
[29.195] VM:-CR t=30
[29.32] SHUTTING DOWN SIM SERVER
[29.32] VM:RTMSh=136,o=0xFFDB800,p=net_rim_bb_simphonebook
[29.32] VM:DPRMv=1
[29.32] AM: Exit net_rim_bb_simphonebook(324)
[29.32] Process net_rim_bb_simphonebook(324) cleanup started
[29.32] VM:EVTOv=7680,w=24
[29.32] SimPhoneBook - Cleanup runnable
[29.32] 04/11 08:53:57.202 BBM FutureTask Execute: net.rim.device.apps.internal.qm.bbm.platform.BBMPlatformM[29.32] anagerImpl$3@c8e6c15d
[29.32] 04/11 08:53:57.202 BBM FutureTask Finish : net.rim.device.apps.internal.qm.bbm.platform.BBMPlatformM[29.32] anagerImpl$3@c8e6c15d
[29.32] Process net_rim_bb_simphonebook(324) cleanup done
[31.429] IIQS: Indexing unpaused. Device is now idle.
[32.164] THRTL: Out: 0 bytes
[32.164] THRTL: In: 0 bytes
[32.171] BRM:IDL+
[32.171] BRM:SR+
[32.171] BRM:NMC:8663040
[32.171] BRM:JFR:23690380
[32.171] BRM:SR-
[32.171] BRM:NMC:8663040
[32.171] BRM:JFR:23689640
[32.171] BRM:IDL-
[32.171] VM:PISVt=0,h=5314,id=d15a33128dfbb5d9
[32.171] VM:LNTDa=commit,t=0,p=net_rim_bb_facebook_lib,h=177
[38.039] AG,+CPT
[38.039] AG,-CPT
[48.078] Model is not dirty.
[48.078] Completing PersistAndPurgeTask.doRequest() at: Thu Apr 11 08:54:16 GMT 2013
[58.093] AM: Exit net_rim_bb_datatags(291)
[58.093] Process net_rim_bb_datatags(291) cleanup started
[58.093] Process net_rim_bb_datatags(291) cleanup done
[58.093] VM:EVTOv=7680,w=24
[58.093] 04/11 08:54:26.078 BBM FutureTask Execute: net.rim.device.apps.internal.qm.bbm.platform.BBMPlatformM[58.093] anagerImpl$3@143a2c1
[58.093] 04/11 08:54:26.078 BBM FutureTask Finish : net.rim.device.apps.internal.qm.bbm.platform.BBMPlatformM[58.093] anagerImpl$3@143a2c1
[92.109] THRTL: Out: 0 bytes
[92.109] THRTL: In: 0 bytes
[92.109] SmplT: stThu Apr 11 08:55:00 GMT 2013
[92.109] VM:PISVt=0,h=5314,id=d15a33128dfbb5d9
[92.109] VM:LNTDa=commit,t=0,p=net_rim_services_impl,h=113
[92.109] 04/11 08:55:00.398 BBM FutureTask Execute: net.rim.device.apps.internal.qm.bbm.filetransfer.BBMFileT[92.109] ransferManager@d93759e1
[92.117] Media:a:TmbF:2
[92.164] 04/11 08:55:00.460 BBM FutureTask Finish : net.rim.device.apps.internal.qm.bbm.filetransfer.BBMFileT[92.164] ransferManager@d93759e1
[125.921] Exception: java.io.EOFException
[125.921] Error while closing: null
[125.921] [B@fdc0ec05

 I attached therelevant section: top: protocol, url, bottom: Exception

It is an end of file  (EOF) Exception, why this ? which file ?

Please use plain text.
Developer
peter_strange
Posts: 19,604
Registered: ‎07-14-2008
My Device: Not Specified

Re: httpConnection can´t send query, difficulties with setRequestProperty

Let us make sure that you do have some data to send:

 

Add

System.out.println("Length: " + postData.length);

just before

dos.write(postdata,0,postdata.length);

 

Add

System.out.println("Written: " + postData.length);

just after

dos.write(postdata,0,postdata.length);

 

and also replace

dos.write(postdata,0,postdata.length);

with

dos.write(postdata);

 

The one thing I would now be suspicious of is the web server that is serving 192.168.0.110.  Can you confirm that it can handle the data?  But in theory the connection has not been made.  So it shouldn't even see this request. 

Please use plain text.
Contributor
bb_eibl
Posts: 26
Registered: ‎04-09-2013
My Device: not specified

Re: httpConnection can´t send query, difficulties with setRequestProperty

I implemented your suggestions.

 

As a result I just insert the log of BB simulator below: BB confirms that it writes: written: 8 , which is fine,  but then the Server gets stuck, no response code, nothing, simply dead. is that correct ??

 

I am using a third party server and they told me to send requests of the kind I do.

 

The http connection is fine, I can set and get various properties, I checked this with setRequestProperty and getRequestProperty.

 

But the query command seems to let the server get stuck. Is this impression correct ?

 

Which file is causing the EOF exception ?

 

 

Your last suggestion with the log was very helpful, I think. It becomes more clear now what the problem is, do you agree ?

My summary: the protocol is fine, but the query is not, it is the wrong query for the server !! Would you agree. Might this be due to the characterset used ??

 

Can I try a simpler  write() that would allow me to test the server, such that the server can easily understand ? e.g. Can I send httpConn.setProperty or getProperty via the write() command ?? Just to avoid that it is a trivial mistake with the character set. Igf not how can  I check that I use the correct character set for the server. I can read the html page of the server that is no problem, but only if I avois sending the query request.

 

 

[2644.718] Protocol: http
[2644.718] URL: http://192.168.0.110/Heizung.htm;deviceside=true
[2644.718] Length: 8
[2644.718] Written: 8
[2685.132] VM:IGCSc=4
[2685.132] VM:+GC(f)w=10
[2685.195] VM:-GCt=8,b=1,r=0,g=f,w=10,m=0
[2685.195] VM:+CR
[2685.203] VM:-CR t=1
[2685.203] THRTL: Out: 0 bytes
[2685.203] THRTL: In: 0 bytes
[2744.195] THRTL: Out: 0 bytes
[2744.195] THRTL: In: 0 bytes
[2753.156] Exception: java.io.EOFException
[2753.156] Error while closing: null
[2753.156] [B@45661029

 

Please use plain text.
Developer
peter_strange
Posts: 19,604
Registered: ‎07-14-2008
My Device: Not Specified

Re: httpConnection can´t send query, difficulties with setRequestProperty

You told us that the processing got stuck on the write, which it did not.  Can you make sure next time the informaiton you give us is accurate. 

 

If you are not using the debugger to step through this code, then you need to put more logging statements in to confirm which statement the processing is stalling on.  Without this information you are really only guessing.

 

One statement you should check is the response code, it is essential in any http protocol debugging that you know what code the server has given you. 

 

But if you were guessing, then I would guess like you that you are not sending the server something it recognises.  Sorry to say this is something you are going to sort out wth your server people.  They should be able to log the request you sent and tell you what you are doing wrong. 

 

One thing I offten do is print out the headers after I have got the response code.  This will confirm the Server that actually responded.  Here is some sample code:

                        StringBuffer sb = new StringBuffer(250);
                        sb.append("Response: " + Integer.toString(rc) + ", " + httpConn.getResponseMessage());
                        for ( int j = 0; j < 20; j++ ) {
                            // Limit to 20 headers, just to reduce data sent to log.
                            String key = httpConn.getHeaderFieldKey(j);
                            String field = httpConn.getHeaderField(j);
                            if ( key == null ) {
                                // Stop when there are no more!
                                break;
                            }
                            String nextheader = key + " : " + field;
                            sb.append("\n" + nextheader);
                        }
                        System.out.println(sb.toString());

 

I would also do some research so you understand the difference between GET and POST and how POST data can be formatted.  This will help you talking to your Server folks. 

 

Please use plain text.
Contributor
bb_eibl
Posts: 26
Registered: ‎04-09-2013
My Device: not specified

Re: httpConnection can´t send query, difficulties with setRequestProperty

be sure I am checking the debugger very carefully and I will give you very accurate information, as accurate as  I can. The problem with the ECLIPSE debugger ist that I can´t get it in step mode to that point to which it goes in resume mode. But this is another story. One workaround after the other !!

 

What I found out now is that the connection gets screwed up by the setRequestProperty httpConn.setRequestMethod(HttpConnection.POST);  command. I out commented the dos.write(); dos.flush() and dos.close(). please see in the code.  Still exactly the same probelm appears: catch EOF exception.

 

THE CONNECTION TO THE SERVER GETS SCREWED UP BY THE POST COMMAND . Please tell me your opinion: yes or no. Do you have any idea why (!!) this would be the case. Do you have any idea for a workaround.

 

I clearly see the problem on the Blackberry side at the moment, not on the server side. I can connect and see the variables with FIREBUG: see below.

 

The POST command  was my point at the very beginning of this thread.

 

your point is important to me:

"I would also do some research so you understand the difference between GET and POST and how POST data can be formatted.  This will help you talking to your Server folks."

 

Do you have any site you could recommend for this ?

 

I hope you don´t mind, if I am frank:

What I would expect from Blackberry software: it sends the commands by the dos.write() statement, it is able to get an understandable error code and displays the error code  (something without running in the catch). You could not explain to me, why it reaches an EOF exception, I cannot explain either.

 

Nothing of this happens. It is an awkward mass taking a lot of time. I find the topic connecting to servers very hard, not nicely programmed, poorly documented. It would greatly help, if BB would offer a test server, to which everyone could connect just to check the basics in a first instance.

 

SERVER side:

 

I checked the connection to the server with FIREFOX and FIREBUG. These programs are able to connect to the server and I can read the variables their content etc. in FIREBUG. This is the point, BB software should be able to do exactly the same, if not than it will be hard for BB to comepete with the rest of the world.

 

I want to be very clear: Thank´s for your help, I really appreciate a lot, I would be totally lost otherwise.

 

 

 

 

 

code:

System.out.println("Protocol: " + httpConn.getProtocol());

                    	System.out.println("URL: " + httpConn.getURL());
                    	byte postdata [];
                        postdata=query.getBytes();
                        int i=postdata.length; 
                      
                        httpConn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, Integer.toString(postdata.length));
                        httpConn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE,"text/html");
                        httpConn.setRequestMethod(HttpConnection.POST);
                        //OutputStream dos =  httpConn.openOutputStream();
                        //System.out.println("Length: " + postdata.length);
                          //dos.write(postdata);
                          //System.out.println("Written: " + postdata.length);
                          
                        
                        // dos.flush();
                         //dos.close();
                         int param15= httpConn.getResponseCode();
                         if(param15==HttpConnection.HTTP_OK) {

 

Please use plain text.
Developer
peter_strange
Posts: 19,604
Registered: ‎07-14-2008
My Device: Not Specified

Re: httpConnection can´t send query, difficulties with setRequestProperty

I suspect you are a little frustrated, given the tone of your POST.   Being honest, I think this frustration comes from (a) not really understanding what you are doing and (b) seeing what you think to be inconsistent or confusing results.  And while I understand that frustration, for me it is actually not justified.  Let me pick through your Post to explain more. 

 

" I can´t get it in step mode"

I don't have this sort of problem myself when debugging code similar to what you have.  System.out.println is very useful anyway because it doesn't require any intervention. 

 

"THE CONNECTION TO THE SERVER GETS SCREWED UP BY THE POST COMMAND"

For me this means the server can't handle the POST you have sent it.  I suspect the connectino is fine. 

 

"I clearly see the problem on the Blackberry side at the moment"

While I suspect this is correct, I would argue this is because you have not coded the BlackBerry side correctly to match what your server requires. 

 

"The POST command  was my point at the very beginning of this thread"

Was it?  Your subject and the post at the beginning seem to be to be focused on the setRequestProperty.  But this is historical, let us nove forward from where we are. 

 

"Do you have any site you could recommend for this ?"

Wikipedia is my goto place for information, if that is not clear I look else where.  There are loads of places that talk about http, including this place:

http://www.w3schools.com/tags/ref_httpmethods.asp

 

"What I would expect from Blackberry software: it sends the commands by the dos.write() statement,"

No it doesn't, it doesn't send anything until you request a response, see here

http://www.blackberry.com/developers/docs/7.1.0api/

Moreover the 'commands' it sends are just bytes, these bytes have to properly formatted so that the Server understands them.  I can't tell you what your Server needs, you have to ask your Server folks.  There are many many different formats for data that is Posted to a Web Server. 

 

"it is able to get an understandable error code and displays the error code"

I understood that this was working correctly for you when you did a GET.  So is problem in the BlackBerry or in your code?

 

"You could not explain to me, why it reaches an EOF exception"

I haven't tried to explain it.  I was not prepared to speculate until I knew which line was actually throwing this exception.  I am pretty sure I know what the issue is here, but I think you can find it yourself.  And if you do it you will learn how to do this, which I think is always a good thing.  But it is frustrating. 

 

"It is an awkward mass taking a lot of time."

Alternatively, you could view it as a learning experience. 

 

"I find the topic connecting to servers very hard"

You are not the only one.  There is a serious amount of background reading to be done because of the way the networking as evolved over the years.  Here is a place to start:

http://developer.blackberry.com/java/documentation/intro_networking_1984362_11.html

At present you are just communicating from a SImulator,  Hate to tell you, but doing this on a real device requires a whole new set of knowledge. 

 

"if BB would offer a test server"

Most people, trying to use POST connections are supplying data in a format that would only be recognized by their Server.  Any web site (I normally use Google) can be used to test a GET request.  So I don't think this would help. 

 

"I checked the connection to the server with FIREFOX"

Unless you have set up a special web page in Firefox, then Firefox is just doing GETs.  So you haven't really tested it in the same way as you were testing on the BlackBerry.  In addition, al lot of Web Servers process the requests and tailor the response based on the Host information that they get in the header with the http request (typically called the user Agent).  Since, you are not sending this in your BlackBerry request, this could explain why you see different results.  I don't know.  Your Server folks should. 

 

Have you tried the URL on the Browser in the Simulator?

 

"and FIREBUG"

Sorry I don't know this tool.  But once again, you are almost certainly doing GETS, not POSTs. 

 

"This is the point, BB software should be able to do exactly the same, if not than it will be hard for BB to compete with the rest of the world."

BB Software can do exactly the same.  But you need to understand what to code.  BB provides a relatively low level API for http connection.  Browser's provide high level.  Unless you understand how the low level http protocol actually works, you will struggle to use it.  You want to try writing TCP/IP level communication software - you will grateful for http!

 

Before I make any more suggestions, I recommend that you put the response header debugging code that I requested in, and also find out exactly which line is causing the EOF Exception.  This will help you move forward. 

Please use plain text.
Contributor
bb_eibl
Posts: 26
Registered: ‎04-09-2013
My Device: not specified

Re: httpConnection can´t send query, difficulties with setRequestProperty

Thank´s very much for your reply!! Very useful again, it reminded me of a problem that I have had ever since. Let me briefly explain it:

 

I can access that server by PC (IE, Firefox), by Android smartphone and Android tablet, no problem at all, I can do all operations  with it , it has plenty but, .... not with the BB torch (real device, not simulator) I am using. I cannot even login!! I cannot acces it with the simulator (+ the browser implemented there) either. Isn´t that strange !! The server is in my home, but its a third party server, I did not program it.

 

 

What I get on the BB browsers  is just the starting page for inserting the username + password but then it goes to an error message: network not avialable.

 

It seems that BB has general difficulties handling this server, I could not find out why.

 

I am a Blackberry fan ever since , but it is hard to stick to it, if you see this.

 

The only way I can acces this server  by blackberry is via my program that I am sharing with you. I can send user and password and see the page etc etc. I was proud to get there !! dispite your remarks :smileyhappy: never mind .

 

Does this mean anything to you ?? I don´t know if these two problems ( BB browser problem, and my program) are related.

 

Sorry for net telling this earlier, but it slipped somehow out of my mind, I amprogramming on 4 platforms  simultaneously:  Java, Fortran, Matlab, ETS4,  with more than 15 different devices....

 

By the way, I am not bound to the POST satatement, wouldn´t it be much better to switch to GET. I checked your references. I do not see a reason why not switching to GET. I don´t need the POST features, do I. How would the code look like if I was using GET instead of POST ? Send the query and the recieve the answer ?

 

You mentioned the acces to Google, could you give me some code to test this access. That would be great !!

 

 

 

 

 

 

Please use plain text.
Developer
peter_strange
Posts: 19,604
Registered: ‎07-14-2008
My Device: Not Specified

Re: httpConnection can´t send query, difficulties with setRequestProperty

Sorry in a rush so a brief response.

 

Some minor points:

 

a) If you expect to use the 3rd party server they should provide you with some documentation

 

b) If you have Android code accessing this Server, the BB code will be very similar.

 

c) I can't tell you if a POST or a GET will work on your Server.  It depends on what it supports.  GET is typically easier to code. 

 

If you code a a GET, you put all the parameters on the URL, so you get a really long URL like this

http://supportforums.blackberry.com/t5/Java-Development/httpConnection-can-t-send-query-difficulties...

and the parameters after the URL are what specifies the action to be taken

 

A POST will have a short URL, but the parameters are passed in a formatted package, that the the Server understands.  These are passed as value pairs, but can also be JSON or event just a stream of bytes - it depends on what the Server needs or understands. 

 

d) You are struggling accessing your own server.  Try changing the code to access something like Google - i.e. www.google.com.

 

e) I have not yet seen the results with the logging in I suggested. I would like to see that.

 

Please use plain text.
Developer
peter_strange
Posts: 19,604
Registered: ‎07-14-2008
My Device: Not Specified

Re: httpConnection can´t send query, difficulties with setRequestProperty

[ Edited ]

Just had a thought here.  Is the reason you are trying to do this in code because the Browser doesn't work?  If the Browser worked you would use that?  And the Browser does work on Android?

 

Reason I ask is becuase I'm guessing this is why you don't know how the Server works.  Most Servers that are expecting to accessed programatically will publish details. In this case the Server devoloper probablty don't expect the Server to be accessed in this way, and so don't publish anything, except a URL to start on.

 

In this case, you can in fact still create code that works like a Browser does - but you have to be able to do what the Browser does.  So for example, in the Browser screen displaya a userid and a password abd a Sign-in Button, then you can view the html for the page, look at the processing associated with the Button and figure out if it is Posting (it probably is) and the data that it is sending.  But to do this you need to be able reverse engineer the html pages.  This is not easy. 

 

Anyway, interested to know if this is the case.

Please use plain text.
Contributor
bb_eibl
Posts: 26
Registered: ‎04-09-2013
My Device: not specified

Re: httpConnection can´t send query, difficulties with setRequestProperty

You are right: I am doing the programming because the BB browser cannot handle the server. But there are also other reasons for me. Android based devices can handle the server with their browser, I have not done any programming yet on Android, but I will.

 

I changed the horses: I SWITCHED TO GET:  I wanted to check that I can at least set up a query on Google and get the results correctly.

 

 

I insert the relevant code, there are 2 queries inside

String httpURL = "http://www.google.de/search?q=hello;deviceside=true";
                    HttpConnection httpConn = (HttpConnection)Connector.open(httpURL);
                    
                    int status = httpConn.getResponseCode();
                    
                    //*************************
                    String query="http://www.google.de/search?q=not hello";
                    System.out.println("Protocol: " + httpConn.getProtocol());

                	System.out.println("URL: " + httpConn.getURL());
                	byte postdata [];
                    postdata=query.getBytes();
                    int i=postdata.length; 
                  
                   // httpConn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, Integer.toString(postdata.length));
                    //httpConn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE,"text/html");
                   // httpConn.setRequestMethod(HttpConnection.GET);
                    OutputStream dos =  httpConn.openOutputStream();
                    System.out.println("Length: " + postdata.length);
                      dos.write(postdata);
                      System.out.println("Written: " + postdata.length);
                    
                    
                    
                    //*****************************
                    String contentType_1 = httpConn.getHeaderField("WWW-Authenticate");
                    //String header="";
                    //httpConn.getHeaderField(header);
                    status = httpConn.getResponseCode();
                    int iu=1;
                    
                    
                    if (status == HttpConnection.HTTP_OK)

 

BB log:

[89.765] Protocol: http
[89.765] URL: http://www.google.de/search?q=hello;deviceside=true
[89.773] Length: 39
[89.773] Written: 39

 I can view the content of the the input stream:

[69859bytes received]hello - Google-Suche#gb{font:13px/27px Arial,sans-serif;height:30px}#gbz,#gbg{position:absolute;white-space:nowrap;top:0;height:30px;z-index:1000}#gbz{left:0;padding-left:4px}#gbg{right:0;padding-right:5px}#gbs{background:transparent;position:absolute;top:-999px;visibil

 I would like have access to the data only, I am not interested in the font information What do I have to do. Could you please give me a hint.

 

I want only to receive the data from google, not the formating, something like "hello you" , "hello Sir" that matches the query string. What do I need to do for this ?

 

In my code there are two query strings: Does the second make sense ? I could not check it easily by the debugger. If it was not correct what should I code instead.

 

 

 

 

 

Please use plain text.