Hi,
See in the following example code ( modified from an older version
of
https://developer.sony.com/downloads/camera-file/sony-camera-remote-api-beta-sdk/
)
In the httpPost(...) method there is an example code to count the total
number of bytes downloaded as well as show elapsed time measurements ( in
milliseconds ) that I mentioned.
The code is not tested, it is just provided as an example...
Hope that helps.
Regards
-------------
/*
* Copyright 2014 Sony Corporation
*/
package com.example.sony.cameraremote.utils;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
/**
* Simple HTTP Client for sample application.
*/
public final class SonyHttpClient {
private static final String TAG = SonyHttpClient.class.getSimpleName();
private static final int DEFAULT_CONNECTION_TIMEOUT = 10000; // [msec]
private static final int DEFAULT_READ_TIMEOUT = 10000; // [msec]
private SonyHttpClient() {
}
/**
* Send HTTP GET request to the indicated url. Then returns response as
* string.
*
* @param url request target
* @return response as string
* @throws IOException all errors and exception are wrapped by this
* Exception.
*/
public static String httpGet(String url) throws IOException {
return httpGet(url, DEFAULT_READ_TIMEOUT);
}
/**
* Send HTTP GET request to the indicated url. Then returns response as
* string.
*
* @param url request target
* @param timeout Request timeout
* @return response as string
* @throws IOException all errors and exception are wrapped by this
* Exception.
*/
public static String httpGet(String url, int timeout) throws
IOException {
HttpURLConnection httpConn = null;
InputStream inputStream = null;
// Open connection and input stream
try {
final URL urlObj = new URL(url);
httpConn = (HttpURLConnection) urlObj.openConnection();
httpConn.setRequestMethod("GET");
httpConn.setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT);
httpConn.setReadTimeout(timeout);
httpConn.connect();
int responseCode = httpConn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
inputStream = httpConn.getInputStream();
}
if (inputStream == null) {
Log.w(TAG, "httpGet: Response Code Error: " + responseCode
+ ": " + url);
throw new IOException("Response Error:" + responseCode);
}
} catch (final SocketTimeoutException e) {
Log.w(TAG, "httpGet: Timeout: " + url);
throw new IOException();
} catch (final MalformedURLException e) {
Log.w(TAG, "httpGet: MalformedUrlException: " + url);
throw new IOException();
} catch (final IOException e) {
Log.w(TAG, "httpGet: " + e.getMessage());
if (httpConn != null) {
httpConn.disconnect();
}
throw e;
}
// Read stream as String
BufferedReader reader = null;
try {
StringBuilder responseBuf = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(inputStream));
int c;
while ((c = reader.read()) != -1) {
responseBuf.append((char) c);
}
return responseBuf.toString();
} catch (IOException e) {
Log.w(TAG, "httpGet: read error: " + e.getMessage());
throw e;
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
Log.w(TAG, "IOException while closing BufferedReader");
}
try {
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e) {
Log.w(TAG, "IOException while closing InputStream");
}
}
}
/**
* Send HTTP POST request to the indicated url. Then returns response as
* string.
*
* @param url request target
* @param postData POST body data as string (ex. JSON)
* @return response as string
* @throws IOException all errors and exception are wrapped by this
* Exception.
*/
public static String httpPost(String url, String postData) throws
IOException
{
return httpPost(url, postData, DEFAULT_READ_TIMEOUT);
}
/**
* Send HTTP POST request to the indicated url. Then returns response as
* string.
*
* @param url request target
* @param postData POST body data as string (ex. JSON)
* @param timeout Request timeout
* @return response as string
* @throws IOException all errors and exception are wrapped by this
* Exception.
*/
public static String httpPost(String url, String postData, int timeout)
throws IOException
{
HttpURLConnection httpConn = null;
OutputStream outputStream = null;
OutputStreamWriter writer = null;
InputStream inputStream = null;
long endWriteTimestamp = 0;
long getResponseCodeTimestamp = 0;
long endReadResponseTimestamp = 0;
int responseCode = 0;
// Open connection and input stream
try {
final URL urlObj = new URL(url);
httpConn = (HttpURLConnection) urlObj.openConnection();
httpConn.setRequestMethod("POST");
httpConn.setConnectTimeout(DEFAULT_CONNECTION_TIMEOUT);
httpConn.setReadTimeout(timeout);
httpConn.setDoInput(true);
httpConn.setDoOutput(true);
outputStream = httpConn.getOutputStream();
writer = new OutputStreamWriter(outputStream, "UTF-8");
writer.write(postData);
writer.flush();
endWriteTimestamp = System.currentTimeMillis();
writer.close();
writer = null;
outputStream.close();
outputStream = null;
responseCode = httpConn.getResponseCode();
getResponseCodeTimestamp = System.currentTimeMillis();
if (responseCode == HttpURLConnection.HTTP_OK)
{
inputStream = httpConn.getInputStream();
}
if (inputStream == null)
{
Log.w(TAG, "httpPost: Response Code Error: " + responseCode
+ ": " + url);
throw new IOException("Response Error:" + responseCode);
}
} catch (final SocketTimeoutException e)
{
Log.w(TAG, "httpPost: Timeout: " + url);
throw new IOException();
} catch (final MalformedURLException e)
{
Log.w(TAG, "httpPost: MalformedUrlException: " + url);
throw new IOException();
} catch (final IOException e)
{
Log.w(TAG, "httpPost: IOException: " + e.getMessage());
if (httpConn != null)
{
httpConn.disconnect();
}
throw e;
} finally {
try {
if (writer != null)
{
writer.close();
}
} catch (IOException e)
{
Log.w(TAG, "IOException while closing OutputStreamWriter");
}
try {
if (outputStream != null)
{
outputStream.close();
}
} catch (IOException e) {
Log.w(TAG, "IOException while closing OutputStream");
}
}
// Read stream as String
BufferedReader reader = null;
try {
StringBuilder responseBuf = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(inputStream));
final int BUFFER_SIZE = 65536;
char[] buffer = new char[ BUFFER_SIZE ];
int i = 0;
int totalRead = 0;
while( ( i = reader.read(buffer, 0, BUFFER_SIZE)) != -1 )
{
responseBuf.append( buffer, 0, i );
totalRead += i;
}
endReadResponseTimestamp = System.currentTimeMillis();
logResponse( "httpPost() String url = " + url + " postData = "
+ postData +
" Execute command elapsed = " + ( getResponseCodeTimestamp -
endWriteTimestamp ) +
" Total write and read elapsed = " + ( endReadResponseTimestamp
- endWriteTimestamp ) +
" Total read elapsed = " + ( endReadResponseTimestamp -
getResponseCodeTimestamp ) +
" totalRead bytes = " + totalRead +
" endWriteTimestamp = " + endWriteTimestamp +
" getResponseCodeTimestamp = " + getResponseCodeTimestamp +
" endReadResponseTimestamp = " + endReadResponseTimestamp +
" responseCode = " + responseCode );
return responseBuf.toString();
} catch (IOException e) {
Log.w(TAG, "httpPost: read error: " + e.getMessage());
throw e;
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
Log.w(TAG, "IOException while closing BufferedReader");
}
}
}
private void logResponse( String logMessage )
{
Log.w(TAG, logMessage );
}
}
On Wednesday, March 16, 2016 at 9:43:18 PM UTC+11, Mustafa Mohammadi wrote:
>
> Thanks for invaluable answer, I like the way you thinking. I will try what
> u said but here is a question: how to measure the number of total bytes
> downloaded?
> Thanks.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/71a5f2d2-f9bb-4b37-8885-ffeeb0d883ff%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.