如何将 publishProgress 与 httpPost 一起使用?我看到了 urlOpenConnection 的例子,但没有看到 HttpPost。是否可以将 publishProgress 与我的代码结构一起使用(我希望查看下载状态)?
我的异步任务子类
class SendData extends AsyncTask<String, Void, JSONObject> {
@Override
protected void onPreExecute() {
pd.setTitle("Loading...");
}
@Override
protected JSONObject doInBackground(String... option) {
JSONObject json = null;
UserFunctions u = new UserFunctions();
json = u.sendAdd(option);
return json;
}
@Override
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
*/
do some job
/*
}
}
我的 json 解析器
public JSONArray getJSONFromUrl(String url, List<NameValuePair> params,
int check) {
HttpResponse httpResponse = null;
InputStream is = null;
JSONArray jObj1 = null;
String json = "";
HttpClient httpClient = null;
if (isOnline()) {
// pd.show();
// // Making HTTP request
String u = url;
u = u + "?";
try {
// defaultHttpClient
httpClient = UILApplication.getHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
for (int i = 0; i < params.size(); i++) {
u = u + params.get(i).getName() + "="
+ params.get(i).getValue() + "&";
}
Log.d("URL", u);
httpResponse = httpClient.execute(httpPost);
List<Cookie> cookies = ((AbstractHttpClient) httpClient)
.getCookieStore().getCookies();
if (cookies.isEmpty()) {
Log.d("COOK", "response - none");
} else {
for (int i = 0; i < cookies.size(); i++) {
Log.d("COOK", "response - " + cookies.get(i).toString());
}
}
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
Log.d("data is sent", "true");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
} catch (ClientProtocolException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
Log.d("wait", "true");
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
// Log.d("JSON line", line);
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
return null;
}
// try parse the string to a JSON object
try {
jObj1 = new JSONArray(json);
} catch (JSONException e) {
e.printStackTrace();
Log.e("JSON Parser", "Error parsing data " + e.toString());
return null;
}
if (json.contains("error:2")) {
Log.e("JSON", jObj1.toString());
HttpClientFactory.killSession();
UILApplication.login = 2;
return null;
}
if (jObj1 != null) {
Log.d("JSON", jObj1.toString());
}
return jObj1;
}
return null;
}
用户函数 - 为 json 解析器生成 NaValuePair 列表。
public JSONObject sendAdd(options) {
// Building Parameters
Log.d("parsing data to sendAdd", "true");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("action", "add_notice"));
*/
more parameters
*/
}
请不要使用 url.openConection 提供代码示例 :)
编辑 我很感兴趣如何获取a
将用于 publishProgress(a) 的值。我知道我可以手动设置它,例如每秒递增一次,但我希望它代表下载状态。