Android水平进度条无法获取JSON

Android水平进度条无法获取JSON,android,json,android-progressbar,Android,Json,Android Progressbar,我正试图通过显示百分比来设置进度条,以从服务器获取大量JSON,但它不起作用。我不知道这一点。所以请从头开始帮助我 Test.java public class Test extends AppCompatActivity { // Progress Dialog private ProgressDialog pDialog; private Toolbar mToolbar; public static final int progress_bar_ty

我正试图通过显示百分比来设置进度条,以从服务器获取大量JSON,但它不起作用。我不知道这一点。所以请从头开始帮助我

Test.java

   public class Test extends  AppCompatActivity {
    // Progress Dialog
    private ProgressDialog pDialog;
    private Toolbar mToolbar;
    public static final int progress_bar_type = 0;
    List<NameValuePair> params;
    // File url to download
    private static String file_url = "http://dcntv.in:3035/area_list";
    Connect cn = new Connect();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("opid", "19"));
        new DownloadFileFromURL().execute(file_url);
    }

    /**
     * Background Async Task to download file
     * */
    class DownloadFileFromURL extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread
         * Show Progress Bar Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(progress_bar_type);
        }

        /**
         * Downloading file in background thread
         * */
        @Override
        protected String doInBackground(String...urls) {
                return cn.readJSONFeed(urls[0],params);
        }

        /**
         * Updating progress bar
         * */
        protected void onProgressUpdate(String... progress) {
            // setting progress percentage
            pDialog.setProgress(Integer.parseInt(progress[0]));
        }

        /**
         * After completing background task
         * Dismiss the progress dialog
         * **/
        @Override
        protected void onPostExecute(String result) {
            Dialog d = new Dialog(Test.this);
            TextView tv = new TextView(Test.this);
            tv.setText(result.toString());
            ScrollView sv = new ScrollView(Test.this);
            sv.addView(tv);
            d.setContentView(sv);
            d.show();
            // dismiss the dialog after the file was downloaded
            dismissDialog(progress_bar_type);
        }

    }

    /**
     * Showing Dialog
     * */
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case progress_bar_type: // we set this to 0
                pDialog = new ProgressDialog(this);
                pDialog.setMessage("Downloading file. Please wait...");
                pDialog.setIndeterminate(false);
                pDialog.setMax(100);
                pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                pDialog.setCancelable(true);
                pDialog.show();
                return pDialog;
            default:
                return null;
        }
    }


}
公共类测试扩展了AppCompatActivity{
//进度对话框
私人对话;
私有工具栏mToolbar;
公共静态最终整数进度条类型=0;
列出参数;
//要下载的文件url
私有静态字符串文件\u url=”http://dcntv.in:3035/area_list";
Connect cn=新连接();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
params=新的ArrayList();
参数添加(新的BasicNameValuePair(“opid”、“19”);
新建DownloadFileFromURL().execute(文件url);
}
/**
*要下载文件的后台异步任务
* */
类DownloadFileFromURL扩展异步任务{
/**
*在启动后台线程之前
*显示进度条对话框
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
显示对话框(进度条类型);
}
/**
*在后台线程中下载文件
* */
@凌驾
受保护的字符串doInBackground(字符串…URL){
返回cn.readJSONFeed(URL[0],参数);
}
/**
*更新进度条
* */
受保护的void onProgressUpdate(字符串…进度){
//设置进度百分比
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
*完成后台任务后
*关闭进度对话框
* **/
@凌驾
受保护的void onPostExecute(字符串结果){
对话框d=新对话框(Test.this);
TextView tv=新的TextView(Test.this);
tv.setText(result.toString());
ScrollView sv=新的ScrollView(Test.this);
sv.addView(电视);
d、 setContentView(sv);
d、 show();
//下载文件后关闭对话框
解雇对话框(进度条类型);
}
}
/**
*显示对话框
* */
@凌驾
受保护的对话框onCreateDialog(int id){
开关(id){
案例进度条类型://我们将其设置为0
pDialog=新建进度对话框(此对话框);
setMessage(“正在下载文件,请稍候…”);
pDialog.setUndeterminate(假);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_水平);
pDialog.setCancelable(真);
pDialog.show();
返回pDialog;
违约:
返回null;
}
}
}
Connect.java(用于cn对象)

公共类连接{
公共字符串readJSONFeed(字符串URL,列表参数){
StringBuilder StringBuilder=新的StringBuilder();
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(URL);
试一试{
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse response=httpClient.execute(httpPost);
StatusLine StatusLine=response.getStatusLine();
int statusCode=statusLine.getStatusCode();
如果(状态代码==200){
HttpEntity=response.getEntity();
InputStream InputStream=entity.getContent();
BufferedReader reader=新的BufferedReader(
新的InputStreamReader(inputStream));
弦线;
而((line=reader.readLine())!=null){
stringBuilder.append(行);
}
inputStream.close();
}否则{
Log.d(“JSON”,“未能下载文件”);
}
}捕获(例外e){
}
返回stringBuilder.toString();
}
}

进度条弹出,但进度计数不起作用

因为没有从
doInBackground
调用
publishProgress
方法在UI线程上发布进度更新

使用当前代码发布更新进行以下更改:

1.
readJSONFeed
中再添加一个参数,以获取
DownloadFileFromURL
的对象:

    public String readJSONFeed(String URL,List<NameValuePair> params,
                                 DownloadFileFromURL objDownloadFileFromURL) {
     ... ...
     while ((line = reader.readLine()) != null) {
           stringBuilder.append(line);
           // add this line
           objDownloadFileFromURL.publishProgress(stringBuilder.length()); 
      }

  ....
}

您遇到了什么问题?进度条弹出,但进度计数不起作用不是每个人都阅读评论;)将此信息添加到问题中。
    public String readJSONFeed(String URL,List<NameValuePair> params,
                                 DownloadFileFromURL objDownloadFileFromURL) {
     ... ...
     while ((line = reader.readLine()) != null) {
           stringBuilder.append(line);
           // add this line
           objDownloadFileFromURL.publishProgress(stringBuilder.length()); 
      }

  ....
}
return cn.readJSONFeed(urls[0],params,DownloadFileFromURL.this);