开发者

j2me openOutputStream stream already open

Im having difficulties with the HttpConnection posting data to my server. The first time everything goes well. The second time it says; 'Stream already open', but i close everything after the response.

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
                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();
                os.close();
                this.result = "dataSend";                

                //Check response and read data
                int res = this.http.getResponseCode();
                this.result = "Result: " + res;
                if(res == 200){
                    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 
                     开发者_如何学JAVA   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.flush();
                        os.close();
                    }catch(Exception err){
                        err.printStackTrace();
                    }
                }

                /*
                if(http != null){
                    try{
                        http.close();
                    }catch(Exception err){
                        err.printStackTrace();
                    }
                }
                */
            }

        }else{
            this.result = "No connection";
        }
    }    

} 


2 ideas:

  1. Why have you commented out the http.close() in finally block? We should always close HttpConnections.

  2. Don't you call GetResult() from several threads simultaneously? If yes, then make the method synchronized by adding synchronized keyword in its definition.

P.S. I find the design of the class a bit misleading. It's very easy to make a mistake by incorrect usage of it. I'd combine GetSnowheights and GetResult into the only synchronized method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜