大佬教程收集整理的这篇文章主要介绍了android – 如何在上传json数据时按百分比显示进度条状态?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
protected class upload_images extends AsyncTask<String,Integer,String> { ProgressDialog progressDialog; @Override protected void onPreExecute() { super.onPreExecute(); // showDialog(progress_bar_typE); progressDialog = new ProgressDialog(Accept_Report.this); progressDialog.setCancelable(false); // dialog.setCanceledOnTouchOutside(false); progressDialog.seTindeterminate(false); // progressDialog.setMax(100); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); // progressDialog.setProgress(0); progressDialog.setMax(100); // progressDialog.setmessage("Loading ..."); progressDialog.show(); // ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar2); } @Override protected String doInBACkground(String... params) { URL url; httpURLConnection connection = null; String http=Util.URL+"reports/media/create"; try { url = new URL(http); connection = (httpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setrequestMethod("POST"); /* connection.setConnectTimeout(50000); connection.setReadTimeout(50000);*/ connection.setrequestProperty("Content-Type","application/json"); connection.setrequestProperty("Content-Language","en-US"); String encoded = Base64.encodeToString(("app" + ":" + "sFif4au7wet8gpsT0boK1oM2Yud6M1").getBytes("UTF-8"),Base64.NO_WRAp); connection.setrequestProperty("Authorization","Basic " + encoded); connection.setUseCaches(false); connection.setDoOutput(true); connection.connect(); jsonArray = new JSONArray(); right = send_right.toString().replaceAll("\\[","").replaceAll("\\]",""); if((right!=null)&&(right!="")) { JSONObject pnObj = new JSONObject(); pnObj.put("comments",right_cm); pnObj.put("section",right_sec); pnObj.put("pictures",right); jsonArray.put(pnObj); } // return @R_33_10586@lSize; JSONObject jsonParam = new JSONObject(); jsonParam.put("media",jsonArray); //Send request int count = 0; OutputStream wr = connection.getOutputStream(); InputStream inputStream = null; byte[] payload = jsonParam.toString().getBytes("UTF-8"); int @R_33_10586@lSze = payload.length; Log.e("@R_33_10586@l size ","" + @R_33_10586@lSzE); int bytesTransferred = 0; int chunkSize = (2*@R_33_10586@lSzE)/100; Boolean last_loop = false; // publishProgress(0); while (bytesTransferred < @R_33_10586@lSzE) { Log.e("bytes transferred","" + bytesTransferred); int nextChunkSize = @R_33_10586@lSze - bytesTransferred; Log.e("nextchunck",""+nextChunkSizE); //int writer_size = wr.toString().getBytes("UTF-8").length; Log.e("chunk size","" + chunkSizE); if (nextChunkSize > chunkSizE) { nextChunkSize = chunkSize; } wr.write(payload,bytesTransferred,nextChunkSizE); bytesTransferred += nextChunkSize; Log.e("byte",""+wr.toString().getBytes("UTF-8").length); Log.e("progress-transferred","" + bytesTransferred +" @R_33_10586@l "+@R_33_10586@lSzE); double cal = (( (doublE)bytesTransferred / (doublE) @R_33_10586@lSzE) * 100); double rounded = (doublE) Math.round(cal * 100.0) / 100.0; Log.e("progress",""+(int)rounded); publishProgress((int)rounded); wr.flush(); wr.close(); }catch(Exception E){ Log.d("Exception",e.toString()); } }*/ Log.e("While loop exit",""); /* wr.flush (); wr.close();*/ }catch (OutOfMemoryError E) { e.printStackTrace(); } //Get Response StringBuilder sb = new StringBuilder(); httpResultimage =connection.getResponseCode(); Log.e("res",""+httpResultimagE); if(httpResultimage==204) { BufferedReader br = new BufferedReader(new InputStreamReader(connection.geTinputStream(),"utf-8")); String line = null; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); System.out.println("" + sb.toString()); }else{ } } catch (Exception E) { e.printStackTrace(); return null; } finally { if(connection != null) { connection.disconnect(); } } return null; } @Override protected void onProgressupdate(Integer... values){ super.onProgressupdate(values); // Log.e("dfsf",""+values[0]); progressDialog.setProgress(values[0]); // progressDialog.setProgress(values[0]); } @Override protected void onPostExecute(String result) { if (httpResultimage==204) { progressDialog.dismiss(); } } }
private class UploadFileToServer extends AsyncTask<Void,String> { @Override protected void onPreExecute() { // setTing progress bar to zero progressBar.setProgress(0); super.onPreExecute(); } @Override protected void onProgressupdate(Integer... progress) { // Making progress bar visible progressBar.setVisibility(View.VISIBLE); // updating progress bar value progressBar.setProgress(progress[0]); // updating percentage value txtPercentage.setText(String.valueOf(progress[0]) + "%"); } @Override protected String doInBACkground(Void... params) { return uploadFile(); } @SuppressWarnings("deprecation") private String uploadFile() { String responseString = null; httpClient httpclient = new DefaulthttpClient(); httpPost httppost = new httpPost(Config.FILE_UPLOAD_URL); try { Androidmultipartentity entity = new Androidmultipartentity( new ProgressListener() { @Override public void transferred(long num) { publishProgress((int) ((num / (float) @R_33_10586@lSizE) * 100)); } }); File sourceFile = new File(filePath); // Adding file data to http body entity.addPart("image",new FileBody(sourceFilE)); // Extra parameters if @R_489_8337@ant to pass to server entity.addPart("website",new StringBody("www.androidhive.info")); entity.addPart("email",new StringBody("abc@gmail.com")); @R_33_10586@lSize = entity.getContentLength(); httppost.setEntity(entity); // Making server call httpResponse response = httpclient.execute(httppost); httpentity r_entity = response.getEntity(); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 200) { // Server response responseString = EntityUtils.toString(r_entity); } else { responseString = "Error occurred! http Status Code: " + statusCode; } } catch (ClientProtocolException E) { responseString = e.toString(); } catch (IOException E) { responseString = e.toString(); } return responseString; } @Override protected void onPostExecute(String result) { Log.e(tag,"Response from server: " + result); // showing the server response in an alert dialog showAlert(result); super.onPostExecute(result); } }
以上是大佬教程为你收集整理的android – 如何在上传json数据时按百分比显示进度条状态?全部内容,希望文章能够帮你解决android – 如何在上传json数据时按百分比显示进度条状态?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。