I am using this solution to make server request.
String message = URLEncoder.encode("my message", "UTF-8");
try {
// instantiate the URL object with the target URL of the resource to
// request
URL url = new URL("http://www.example.com/comment");
// instantiate the HttpURLConnection with the URL object - A new
// connection is opened every time by calling the openConnection
// method of the protocol handler for this URL.
// 1. This is the point where the connection is opened.
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
// set connection output to true
connection.setDoOutput(true);
// instead of a GET, we're going to send using method="POST"
connection.setRequestMethod("POST");
// instantiate OutputStreamWriter using the output stream, returned
// from getOutputStream, that writes to this connection.
// 2. This is the point where you'll know if the connection was
// successfully established. If an I/O error occurs while creating
// the output stream, you'll see an IOException.
OutputStreamWriter writer = new OutputStreamWriter(
connection.getOutputStream());
// write data to the connection. This is data that you are sending
// to the server
// 3. No. Sending the data is conducted here. We established the
// connection with getOutputStream
writer.write("message=" + message);
// Closes this output stream and releases any system resources
// associated with this stream. At this point, we've sent all the
// data. Only the outputStream is closed at this point, not the
// actual connection
writer.close();
// if there is a response code AND that response code is 200 OK, do
// stuff in the first if block
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
// OK
// otherwise, if any other status code is returned, or no status
// code is returned, do stuff in the else block
} else {
// Server returned HTTP error code.
}
} catch (MalformedURLException e) {
// ...
} catch (IOException e) {
// ...
}
Everything is working fine. But my server team mentions that there are multiple server request is hitting from one device. you are creating multiple open connections. Try to open one connection and send all the request from that connection. Do not open multiple openConnection().
I verified many sites regarding this. But no luck. Could anybody let me know is it possible to send all the request in on connection??
vhttpReadStream = httpsConnection.getInputStream();
vinpBytes = new byte[CVBUFFSIZE];
vbyteOs = new ByteArrayOutputStream(CVBUFFSIZE);
try {
while ((vintToRead = vhttpReadStream.read(vinpBytes)) != -1) {
vbyteOs.write(vinpBytes, 0, vintToRead);
cvtotalBytes += vintToRead;
}
} catch (Exception e) {
// #ifdef j2meuix.sop
Log.e(FILENAME, "output in Exception : " + vbyteOs.toString());
Log.e(FILENAME, "Exception in parsing resp : " + e);
// #endif
}