0

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

happy
  • 2,550
  • 17
  • 64
  • 109
  • 1). in how many time connection will close...?? 2). When connection will disconnected "between sending data" or "else"...? – V.J. Dec 30 '11 at 07:36
  • I am using web service in my app.I am able to login into the app and gets response from webservice in form of all user data and then after that get disconnects after that when I send request its shows Request Timed out – happy Dec 30 '11 at 11:56

1 Answers1

0
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 .........

V.J.
  • 9,492
  • 4
  • 33
  • 49