1

如何检查每次上传到服务器的数据量,这是我在 AsyncTask 中调用的代码

public  String conxDatosInPost(String Direccion, ArrayList<NameValuePair> Parametros) throws Exception {
  BufferedReader in = null;
  try {
    HttpClient client = ClienteHttp();
    HttpPost request = new HttpPost(Direccion);
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(Parametros);
    request.setEntity(formEntity);
    HttpResponse response = client.execute(request);
    //long logitud = response.getEntity().getContentLength();
    in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    StringBuffer sb = new StringBuffer("");
    String line = "";
    String NL = System.getProperty("line.separator");
    while ((line = in.readLine()) != null) {
     sb.append(line + NL);
    } 
    in.close();
    String result = sb.toString();
    Log.d("El resultado de cnxposin es :-----------  ",  result +"fn");
    return result;
  } finally {
    if (in != null) {
     try {
      in.close();
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
  }
}

这是我的课程CLienteHttp();

private  HttpClient ClienteHttp() {
  if (mHttpCliente == null) {
     public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds
    mHttpCliente = new DefaultHttpClient();
    final HttpParams params = mHttpCliente.getParams();
    HttpConnectionParams.setConnectionTimeout(params, HTTP_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, HTTP_TIMEOUT);
    ConnManagerParams.setTimeout(params, HTTP_TIMEOUT);
  }
  return mHttpCliente;
}

这是我的类 asyncTask

public class NuevoPostImagen extends AsyncTask<String, Integer, String>{
  @Override
  protected String doInBackground(String... params) {
    objAcceso = new AccesoBd();
    String jsRes="";
    SharedPreferences userDetails = getParent().getSharedPreferences("MisPreferencias", MODE_PRIVATE);
    ArrayList<NameValuePair> ParametrosDeEnvio = new ArrayList<NameValuePair>();
    ParametrosDeEnvio.add(new BasicNameValuePair("from",Integer.toString(userDetails.getInt("Id",0))));
    ParametrosDeEnvio.add(new BasicNameValuePair("idrecurso",Integer.toString(idEvento)));
    ParametrosDeEnvio.add(new BasicNameValuePair("texto","nada"));
    ParametrosDeEnvio.add(new BasicNameValuePair("titulo",txtFTitulo.getText().toString()));
    ParametrosDeEnvio.add(new BasicNameValuePair("imageFile",sImagenBsase64));
    try{
      //here 
      String jes=objAcceso.conxDatosInPost(params[0],ParametrosDeEnvio);
      jsRes = "ok";
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
    }finally{return jsRes;}
  }
  @Override
  public void onProgressUpdate(Integer... args){
    Pro.setProgress(args[0]);
  }
  @Override
  protected void onPostExecute( String result) {
    Pro.dismiss();
    if (result.toString().equals("ok")){
      LlenarEvento();
      dOpciones.dismiss();
      dHacerFoto.dismiss();
    }
  }
  @Override
  protected void onPreExecute() {
    Pro = ProgressDialog.show(getParent(), "", "Enviando",true,true);
    Pro.setCancelable(false);
  }
}

我可以用现有代码更新我的进度条吗?

一旦cliente.execute(httppost)。

我想知道如何放置一个侦听器或其他东西来检查上传的字节数每秒或每次上传它们时更新?我需要根据上传的数量更新我的进度条。

请问,我可以帮忙吗?

4

1 回答 1

0

您可以为此使用异步任务。你必须超越 3 种方法。

doinbackground() onpreexecute() onpostexecute() onProgressUpdate 我认为这将帮助您了解如何去做。如何在android中实现文件上传进度条

于 2012-11-30T17:33:10.030 回答