首页 > 代码库 > Code review(自己的)

Code review(自己的)

以下是原代码:

  1 import android.app.ProgressDialog;
  2 import android.graphics.Bitmap;
  3 import android.graphics.BitmapFactory;
  4 import android.os.AsyncTask;
  5 import android.support.v7.app.AppCompatActivity;
  6 import android.os.Bundle;
  7 import android.view.View;
  8 import android.widget.Button;
  9 import android.widget.ImageView;
 10 
 11 import java.io.ByteArrayInputStream;
 12 import java.io.ByteArrayOutputStream;
 13 import java.io.IOException;
 14 import java.io.InputStream;
 15 import java.net.HttpURLConnection;
 16 import java.net.MalformedURLException;
 17 import java.net.URL;
 18 
 19 public class MainActivity extends AppCompatActivity {
 20     private Button btn;
 21     private ImageView imageView;
 22     private ProgressDialog progressDialog;
 23     private final String IMAGE_PATH = "http://www.baidu.com/img/bd_logo1.png";
 24 
 25     @Override
 26     protected void onCreate(Bundle savedInstanceState) {
 27         super.onCreate(savedInstanceState);
 28         setContentView(R.layout.activity_main);
 29         btn = (Button) findViewById(R.id.btn);
 30         imageView = (ImageView) findViewById(R.id.imageView);
 31         progressDialog = new ProgressDialog(MainActivity.this);
 32         progressDialog.setTitle("提示信息");
 33         progressDialog.setCancelable(false);
 34         progressDialog.setMessage("正在下载,请稍候。。。");
 35 //        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
 36         progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
 37 
 38         btn.setOnClickListener(new View.OnClickListener() {
 39             @Override
 40             public void onClick(View v) {
 41                 new MyAsyncTask().execute(IMAGE_PATH);
 42             }
 43         });
 44 
 45     }
 46 
 47     public class MyAsyncTask extends AsyncTask<String, Integer, byte[]> {
 48         @Override
 49         protected void onPreExecute() {
 50             super.onPreExecute();
 51             progressDialog.show();
 52         }
 53 
 54         @Override
 55         protected byte[] doInBackground(String... strings) {
 56             byte[] image = new byte[] {};
 57             HttpURLConnection conn = null;
 58             InputStream inputStream = null;
 59             ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
 60 
 61             try {
 62                 URL url = new URL(strings[0]);
 63                 conn = (HttpURLConnection)url.openConnection();
 64                 if (conn.getResponseCode() == 200) {
 65                     long file_length = conn.getContentLength();
 66                     long total_length = 0;
 67                     int length = 0;
 68                     byte[] data = http://www.mamicode.com/new byte[1024];
 69                     inputStream = conn.getInputStream();
 70                     while (-1 != (length = inputStream.read(data))) {
 71                         total_length += length;
 72                         byteArrayOutputStream.write(data, 0, length);
 73                         int progress = ((int) (total_length*100/(float)file_length));
 74                         publishProgress(progress);
 75 
 76                         try {
 77                             Thread.sleep(1000);
 78                         } catch (InterruptedException e) {
 79                             e.printStackTrace();
 80                         }
 81                     }
 82                     image = byteArrayOutputStream.toByteArray();
 83                     inputStream.close();
 84                     byteArrayOutputStream.close();
 85                 }
 86             } catch (MalformedURLException e) {
 87                 e.printStackTrace();
 88             } catch (IOException e) {
 89                 e.printStackTrace();
 90             }finally {
 91                 conn.disconnect();
 92             }
 93             return image;
 94         }
 95 
 96         @Override
 97         protected void onProgressUpdate(Integer... values) {
 98             super.onProgressUpdate(values);
 99             progressDialog.setProgress(values[0]);
100         }
101 
102         @Override
103         protected void onPostExecute(byte[] bytes) {
104             super.onPostExecute(bytes);
105             Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
106             imageView.setImageBitmap(bitmap);
107             progressDialog.dismiss();
108         }
109     }
110 }

代码审查结果:

  1、缺少注释

  2、对非常规行为和边界值情况处理没有描述

  3、存在部分重复的代码

 

Code review(自己的)