检查上传到服务器的金额数据

时间:2012-11-30 17:17:02

标签: android android-asynctask android-progressbar

如何检查每次上传到服务器的数据量,这是我在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(httpost)。

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

拜托,我可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

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

doinbackground() onpreexecute() onpostexecute() onProgressUpdate 我认为这将有助于你理解如何做到这一点。 How to implement file upload progress bar in android