Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Blackberry j2me openOutputStream流已打开_Blackberry_Post_Java Me_Outputstream_Httpconnection - Fatal编程技术网

Blackberry j2me openOutputStream流已打开

Blackberry j2me openOutputStream流已打开,blackberry,post,java-me,outputstream,httpconnection,Blackberry,Post,Java Me,Outputstream,Httpconnection,我在HttpConnection向我的服务器发布数据时遇到困难。第一次一切顺利。第二次,它说:流已经打开,但我在响应后关闭所有内容 这是我的密码: import javax.microedition.io.Connector; import javax.microedition.io.HttpConnection; import javax.microedition.location.*; import java.io.*; class GetSnowheights { Htt

我在HttpConnection向我的服务器发布数据时遇到困难。第一次一切顺利。第二次,它说:流已经打开,但我在响应后关闭所有内容

这是我的密码:

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 
                        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个想法:

为什么要注释掉http.close-in-finally块?我们应该始终关闭http连接

您不是同时从多个线程调用GetResult吗?如果是,则通过在其定义中添加synchronized关键字使方法同步


另外,我觉得这个班级的设计有点误导。不正确地使用它很容易犯错误。我会将GetSnowheights和GetResult合并到唯一的同步方法中。

不工作。还是一样的信息。即使使用了sinchronized voidity,我也不知道该怎么做。你想重用这个。http吗?也就是说,您是否多次调用GetResult而没有获得新的HttpConnection?我认为这是不允许的。这是一个单例类的Internet,它返回一个有效的httpconnection。http在这个类中是私有的,所以是的,我重用它。好的,那么您对GetSnowheights类的每个实例调用GetResult不止一次?这意味着您正在尝试重用HttpConnection。我自己从来没有这样做过,但我觉得这是一个潜在的问题。如果在每次调用GetResult之前重构代码以创建一个新的GetSnowheights实例呢?