Search code examples
web-servicesblackberryblackberry-eclipse-pluginblackberry-jdej2mepolish

Connection get closed after some time


I have used below code for internet connection

HttpConnection httpConn = null;
DataOutputStream dataOS = null;
redemptionUrl = redemptionUrl+ ";deviceside=true";
httpConn = (HttpConnection) Connector.open(redemptionUrl);

httpConn.setRequestProperty("User-Agent",
"Profile/MIDP-1.0, Configuration/CLDC-1.0");
httpConn.setRequestProperty("Connection", "Keep-Alive");
httpConn.setRequestProperty("Content-Language", "en-US");
httpConn.setRequestMethod(HttpConnection.POST);
dataOS = (DataOutputStream) httpConn.openDataOutputStream();
dataOS.flush();
dataOS.close();
DataInputStream dataIS = (DataInputStream) httpConn
.openDataInputStream();
int ch;
sb = new StringBuffer();
System.out.println("%%%% Me here 4 sb is ::" + sb.toString());
while ((ch = dataIS.read()) != -1) {
sb.append((char) ch);
}
// Respeonse
// -------------------------------------------------------------
System.out.println("sb.toString()::" + sb.toString());
String responseData = sb.toString();
dataIS.close();
httpConn.close();

After some time connection is disconnected. what's wrong ,can any one help


Solution

  • public class HttpPostRetriver extends Thread{
    private String _url;
    private StringBuffer _postData;
    private byte[] _data=new byte[512];
    private HttpConnection _httpConnection;
    private OutputStream os;
    private InputStream is;
    
    public HttpPostRetriver(String url) {
        _url = url+UrlSuffix.updataConnectionSuffix();
        try {
          _httpConnection = (HttpConnection)Connector.open(_url);
        } catch(Exception e) {
        }
    }
    
    public String postData() {
         try {
            _httpConnection.setRequestMethod(HttpConnection.POST);
            _httpConnection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
            _httpConnection.setRequestProperty("Content-Language", "en-US");
            _httpConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            os = _httpConnection.openOutputStream();
    
            int rc = _httpConnection.getResponseCode();
            if(rc == HttpConnection.HTTP_OK) {
                  is = _httpConnection.openInputStream();
                  is.read(_data);
            } else {
              _data = null;
            }
       } catch(Exception e) {
           //exception
       }
       return (new String(_data));
    }
    }
    

    Try this code .........