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
Regular Contributor
Posts: 92
Registered: ‎01-31-2012
My Device: curve 8520
My Carrier: vodafone

java.io.IOException: Stream not in setup state in authorization

help me please my connection throws that exception" java.io.IOException: Stream not in setup state" when I send the authorization info to the server and I don't know what I should do !

 

connection = (HttpsConnection) Connector.open(url+getBlackBerryConnectionParams(), Connector.READ,
					true);
			connection.setRequestProperty("User-Agent",
					"Profile/MIDP-2.0 Configuration/CLDC-1.0");
			connection.setRequestProperty("Content-Language", "en-US");

			connection.setRequestProperty("Content-Type",
					"application/x-www-form-urlencoded");
			status = connection.getResponseCode();

			if (connection.getResponseCode() == HttpsConnection.HTTP_UNAUTHORIZED) {
				connection.setRequestProperty("Authorization", "Basic "
						+ new String(getEncode()));
			}
		

 and the url is https connection

Super Contributor
Posts: 278
Registered: ‎11-04-2010
My Device: 4.5 and above
My Carrier: none

Re: java.io.IOException: Stream not in setup state in authorization

Hi,

1. check whether are you calling this in event thread(server connection should not be called in event thread).
2. connectionstring is set properly.
3. which os you are targetting.

Regards,
pp
Regular Contributor
Posts: 92
Registered: ‎01-31-2012
My Device: curve 8520
My Carrier: vodafone

Re: java.io.IOException: Stream not in setup state in authorization

1.it'snot in the main thread
2.what do you mean ?
3.os 6
Super Contributor
Posts: 278
Registered: ‎11-04-2010
My Device: 4.5 and above
My Carrier: none

Re: java.io.IOException: Stream not in setup state in authorization

Hi,

 

if you are targeting OS6, you can use...  ConnectionFactory to get the connection to url.
http://supportforums.blackberry.com/t5/Java-Development/Sample-Code-Using-the-ConnectionFactory-clas... 

 

MyConnectionFactory connFact = new MyConnectionFactory();
ConnectionDescriptor connDesc;
connDesc = connFact.getConnection(url);
connection = (HttpConnection) connDesc.getConnection();

 

And one more thing, is this a post method then,

connection.setRequestMethod(HttpConnection.POST); is needed i think.

 

Regards,

pp

Regular Contributor
Posts: 92
Registered: ‎01-31-2012
My Device: curve 8520
My Carrier: vodafone

Re: java.io.IOException: Stream not in setup state in authorization

[ Edited ]

actually the problem in the authentication now it doesn't throw that exception it gives me 401 and I am using this code and it's giving me the same error , btw the url attached is https and the getBlackBerryConnectionParameters() is a method to attach suffix to the url whether it work on wifi or BIS

 

connection = (HttpsConnection) Connector.open(url
					+ getBlackBerryConnectionParams());
	status = connection.getResponseCode();

			while (keepGoing) {
				switch (status) {
				case (HttpsConnection.HTTP_UNAUTHORIZED): {
					connection.close();

					
					StringBuffer buf = new StringBuffer(username);

					buf.append(':');
					buf.append(password);

					byte[] raw = buf.toString().getBytes();
					encoded = Base64OutputStream.encode(raw, 0, raw.length,
							false, false);
					connection = (HttpsConnection) Connector.open(url
							+ getBlackBerryConnectionParams());

					connection.setRequestProperty("Authorization", new String(
							"Basic " + encoded));
				}
					break;
				case (HttpsConnection.HTTP_OK): {
					keepGoing = false;
				}
					break;
				default:
					keepGoing = false;
					break;
				}
			}

 

Super Contributor
Posts: 278
Registered: ‎11-04-2010
My Device: 4.5 and above
My Carrier: none

Re: java.io.IOException: Stream not in setup state in authorization

Hi,

 

for post method try this,

 

new Thread(new Runnable()  { 
            public void run() {
                if(!InternetUtils.OutOfCoverage()) {
                    //doPost(property);
                    HttpConnection connection = null;
                    InputStream inputStream = null;
                    OutputStream strmOut = null;
                    try {
                        // Add parameters
						URLEncodedPostData oPostData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false);
						oPostData.append("wcf:", "passWcf2012");
                        
                        
                        MyConnectionFactory connFact = new MyConnectionFactory();
                        ConnectionDescriptor connDesc;
                        connDesc = connFact.getConnection(url);
                        connection = (HttpConnection) connDesc.getConnection();
                        
                        // POST data
                        connection.setRequestMethod(HttpConnection.POST); 
                        connection.setRequestProperty("User-Agent", "BlackBerry");///3.2.1");
                        connection.setRequestProperty("Content-Type", "multipart/form-data");
                        connection.setRequestProperty("MIME-Type", "Image/Jpeg");
                        connection.setRequestProperty("Content-Language", "en-US");
                        connection.setRequestProperty("Connection", "close");
                        connection.setRequestProperty("Content-Length", Integer.toString(oPostData.size()));
                        
                        strmOut = connection.openOutputStream();
                        strmOut.write(oPostData.getBytes());
                        
                        strmOut.flush();
                        
                        inputStream = connection.openInputStream();
                        byte[] responseData = new byte[10000];
                        int length = 0;
                        StringBuffer rawResponse = new StringBuffer();
                        while (-1 != (length = inputStream.read(responseData)))
                        {
                            rawResponse.append(new String(responseData, 0, length));
                        }
                        int responseCode = connection.getResponseCode();
                        if (responseCode != HttpConnection.HTTP_OK)
                        {
                            throw new IOException("HTTP response code: "
                                    + responseCode);
                        }
        
                        final String result = rawResponse.toString();
                        System.out.println("Result in  getWedData : "+ result);
                    } catch (final Exception ex)  {
                        String strResult = "Exception (" + ex.getClass() + "): " + ex.getMessage();
                        System.out.println("Exception in  getWedData : "+ strResult);
                    }
                    finally {
                        try {
                            // Clean up
                            if (inputStream != null)
                                inputStream.close();
                            if (connection != null)
                                connection.close();        
                            if (strmOut != null)
                                strmOut.close();   
                        } catch(Exception e){}
                    }
                } else {
                        System.out.println("Network out of coverage.");
                }
            }
        }).start();

 Regards,

pp

Regular Contributor
Posts: 92
Registered: ‎01-31-2012
My Device: curve 8520
My Carrier: vodafone

Re: java.io.IOException: Stream not in setup state in authorization

really great thanks for your help but it writes in the console "Exception in getWedData : Exception (class java.io.IOException): HTTP response code: 401" the same problem Smiley Frustrated
Super Contributor
Posts: 278
Registered: ‎11-04-2010
My Device: 4.5 and above
My Carrier: none

Re: java.io.IOException: Stream not in setup state in authorization

Hi,

am very sorry, I taught you are trying for http post... sorry once again.. try this,
http://docs.blackberry.com/en/developers/deliverables/11938/Use_HTTP_authentication_508958_11.jsp

Regards,
pp
Regular Contributor
Posts: 92
Registered: ‎01-31-2012
My Device: curve 8520
My Carrier: vodafone

Re: java.io.IOException: Stream not in setup state in authorization

I did the same method and it doesn't authenticate , do you think the problem in the server ?
Super Contributor
Posts: 278
Registered: ‎11-04-2010
My Device: 4.5 and above
My Carrier: none