我使用HTTPURLConnection将字符串数据发送到服务器。
JSONObject postDataParams = new JSONObject();
postDataParams.put(key1,a1);
postDataParams.put(key2,a2)
Params = getPostDataString(postDataParams);
getPostDataString()在哪里
public String getPostDataString(JSONObject params) throws Exception {
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator<String> itr = params.keys();
while(itr.hasNext()){
String key= itr.next();
Object value = params.get(key);
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(key, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(value.toString(), "UTF-8"));
}
return result.toString();
}
我使用BufferedWriter将文本写入字符输出流,如下所示:
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
我可以按以下方式计算请求的总大小(以字节为单位):
int Total = Params.getBytes().length;
我的问题是如何计算发送字节的数量,以便能够计算它们之间的比率并通过onUpdateProgress()发布比率
int ratio = (sentBytes*100)/(Total);
publishProgress(ratio);