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
New Developer
digitalhuman
Posts: 13
Registered: ‎10-28-2008
My Device: Bol

Post and recieve data throught HttpConnection

Hi,

 

I'm having problems with posting and recieving response through the HttpConnection. When i post the first time, and read the response it all goes well. The second time it throws an exception; 'Stream already open'. Even when i close everything. Please help me out.

 

Here is my code;

 

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.location.*;
import java.io.*;

class GetSnowheights
{    
    HttpConnection http = null;
    QualifiedCoordinates q = null;
    public String result = "Geen data";
    private boolean running;

    public GetSnowheights(QualifiedCoordinates q) {        
        try
        {
            /*
            this.http = (HttpConnection)Connector.open("http://www.diamond4it.nl/bb/");                
            this.http.setRequestMethod(HttpConnection.POST);
            this.http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            */
            //Internet.getInstance();
            this.http = Internet.getConnection();
        }catch(Exception err){
           err.printStackTrace();
        }
        this.q = q;
        this.result = "Running";
    }
    
    public void GetResult(){
        StringBuffer sb = new StringBuffer();        
        this.result = "GetResult";
        
        if(this.http != null){
            
            OutputStream os = null;
            InputStream is = null;
            try
            {
                //Send request
                if(os != null){ os.close(); }               
                os = this.http.openOutputStream();
                String data = "lat=1&lng=1";
                //String data = "lat=" + this.q.getLatitude() + "&lng=" + this.q.getLongitude();
                os.write(data.getBytes());
                os.flush();                
                this.result = "dataSend";                
                                
                //Check response and read data
                int res = this.http.getResponseCode();
                this.result = "Result: " + res;
                if(res == 200){
                    if(is != null){ is.reset(); }
                    is = this.http.openInputStream();
                    int ch;
                    // Check the Content-Length first 
                    long len = this.http.getLength();
                    if(len!=-1) { 
                        for(int i = 0;i<len;i++){
                            if((ch = is.read())!= -1){
                                sb.append((char)ch);
                            }
                        }
                    } else { 
                        // if the content-length is not available 
                        while ((ch = is.read()) != -1){
                            sb.append((char)ch); 
                        }
                    }
                    is.close();
                }
                
                this.result = sb.toString();

            }catch(Exception err){
                //err.printStackTrace();
                this.result = err.toString() + "\r\n" + err.getMessage();
            }finally{
                if(is != null){
                    try{
                        is.close();
                    }catch(Exception err){
                        err.printStackTrace();
                    }
                }
                if(os != null){
                    try{
                        os.close();
                    }catch(Exception err){
                        err.printStackTrace();
                    }
                }
                
                /*
                if(http != null){
                    try{
                        http.close();
                    }catch(Exception err){
                        err.printStackTrace();
                    }
                }
                */
            }
                    
        }else{
            this.result = "No connection";
        }
    }    
    
} 

 

Meet me at #SAMS: http://www.onsams.com, Twitter: @digital_human, or visit my blog: http://victorangelier.blogspot.com
Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: Post and recieve data throught HttpConnection

I don't think you can reuse an httpConnection.  So move the

this.http = Internet.getConnection();

into the beginning of
GetResult()

and I think it will work fine.