2017-10-06 16 views
1

json에서 다운로드 한 데이터의 비율로 진행률 표시 줄을 표시하려고합니다. 지금은 url에서 데이터를 가져와 다른 클래스의 로컬 데이터베이스에 저장하고이 클래스는 MainActivity에서 호출됩니다. 이제 json url에서 다운로드 파일의 비율로 progressbar을 보여주고 싶습니다.android에서 json get method에서 데이터를 다운로드 할 때 진행률 막대를 설정하십시오.

이 내 코드 doInBackground에서

public class Web_Product { 

Context context; 
List<Variable> list = new ArrayList<Variable>(); 
//List<Variable> list1; 
String url = "https://api.androidhive.info/progressdialog/hive.jpg"; 
URL url1 = null; 
InputStream is1 = null; 
String product_id, product_name, product_image; 
private byte[] logoImage; 
private JSONArray jsonArray; 

public Web_Product(Context context) { 
    this.context = context; 
    Log.e("hello", "Message"); 

} 

public void product_insert() { 
    // new AsyncLogin().execute(); 
    ServiceHandler sh = new ServiceHandler(); 

    // Making a request to url and getting response 
    String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET); 

    Log.e("TEST", "jsonStr:-" + jsonStr); 

    if (jsonStr != null) { 
     try { 

      JSONObject jsonRootObject = new JSONObject(jsonStr); 

      //Get the instance of JSONArray that contains JSONObjects 
      JSONArray jsonArray = jsonRootObject.optJSONArray("product"); 

      //Iterate the jsonArray and print the info of JSONObjects 
      for (int i = 0; i < jsonArray.length(); i++) { 
       Log.e("TEST_P", "in"); 
       JSONObject details = jsonArray.getJSONObject(i); 

       product_id = details.getString("product_id"); 
       product_name = details.getString("product_name"); 
       product_image = details.getString("product_image"); 

       logoImage = getLogoImage(product_image); 

       Variable variable_object = new Variable(); 
       variable_object.setProduct_id(product_id); 
       variable_object.setProduct_name(product_name); 
       variable_object.setProduct_url_image(logoImage); 

       list.add(variable_object); 

      } 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

    } else { 
     Log.e("ServiceHandler", "Couldn't get any data from the url"); 
    } 
    Log.e("TEST1", " Ritunjay" + list.size()); 
    Product_data product = new Product_data(context); 
    product.Insert_Product(list); 
    Log.e("listpo", "" + list); 

} 

public JSONArray json_web_prod() { 
    ServiceHandler sh = new ServiceHandler(); 

    // Making a request to url and getting response 
    String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET); 

    Log.e("TEST", "jsonStr:-" + jsonStr); 

    if (jsonStr != null) { 
     try { 

      JSONObject jsonRootObject = new JSONObject(jsonStr); 

      //Get the instance of JSONArray that contains JSONObjects 
      jsonArray = jsonRootObject.optJSONArray("product"); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 
    return jsonArray; 
}` 

주요 활동

class add extends AsyncTask<String, Integer, String> { 

    ProgressDialog mProgressDialog; 

    @Override 
    protected void onProgressUpdate(Integer... values) { 
     mProgressDialog.setProgress(values[0]); 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     mProgressDialog = new ProgressDialog(MainActivity.this); 
     mProgressDialog.setTitle("Downloading Data..."); 
     mProgressDialog.setIndeterminate(false); 
     mProgressDialog.setMax(100); 
     mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     mProgressDialog.setCancelable(false); 
     mProgressDialog.show(); 
    } 

    @Override 
    protected void onPostExecute(String aVoid) { 
     super.onPostExecute(aVoid); 
     mProgressDialog.dismiss(); 
     flag = true; 
     Intent intent = getIntent(); 
     startActivity(intent); 
     finish(); 

     Log.e("flag , post", "" + flag); 

    } 

    @Override 
    protected String doInBackground(String... params) { 

     web_product = new Web_Product(getApplicationContext()); 
     web_product.product_insert(); 

     return null; 

    } 


}` 
+0

[this] (https://stackoverflow.com/a/4591906/4730040) 게시물을 통해 진행 상황을 업데이트하는 데 도움이 될 것입니다. –

답변

0

, 당신은 또한 publishProgress해야합니다. 진행 표시 줄이 여기에이

protected Long doInBackground(URL... urls) { 
     int count = urls.length; 
     long totalSize = 0; 
     for (int i = 0; i < count; i++) { 
      totalSize += Downloader.downloadFile(urls[i]); 
      publishProgress((int) ((i/(float) count) * 100)); 
      // Escape early if cancel() is called 
      if (isCancelled()) break; 
     } 
     return totalSize; 
    } 

처럼, 업데이트 할 수 있도록 totalSize는 총 남아있는 데이터를 계산하고 publishProgress 공공 것이다 데 도움이 될 것입니다.