2014-06-09 4 views
0

여기에서 progress spinner library 하나를 사용했습니다. params AsyncTask<String, Integer, Double>을 사용하여 인스턴스를 사용할 때 제대로 작동했습니다. 완벽하게 작동하며 UI에서 진행률 회 전자를 보여줍니다. 내가 출력으로 JSONArray을 필요로하는 난 그냥 수정진행 표시 줄이 표시되지 않습니까?

그래서 그것은 PROGRES 스피너를 보여 중지 AsyncTask<String, Integer, JSONArray>

로 수정했습니다. 그래서 나는 더미를 시도했습니다 publishProgress((Integer) ((i/count) * 100)); 메소드 onProgressUpdate을 호출하지만 진도 회전기는 표시되지 않았습니다.

AsyncTask<String, Integer, JSONArray> asynTask = new GetEmployeeDetails(MainActivity.this).execute(""); 
     try { 
      response = asynTask.get(); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } catch (ExecutionException e) { 
      e.printStackTrace(); 
     } 
     Log.i("Resultz >>>>>>",response.toString()); 
     } 

그리고

public class GetEmployeeDetails extends AsyncTask<String, Integer, JSONArray> 
implements OnCancelListener { 
    ProgressHUD mProgressHUD; 
    private int statusCode = 0; 
    private Activity activity; 

    public GetEmployeeDetails(MainActivity activity) { 
    super(); 
    this.activity = activity; 
    } 

    @Override 
    protected void onPreExecute() { 
    mProgressHUD = ProgressHUD.show(activity, "Posting Data...", true, false, 
     this); 
    super.onPreExecute(); 
    } 

    @Override 
    protected JSONArray doInBackground(String... params) { 
    JSONArray resultJSONArray = null; 
    try { 
     resultJSONArray = getData(params[0]); 
    } catch (URISyntaxException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return resultJSONArray; 
    } 

    protected void onPostExecute(JSONArray result) { 
    // pb.setVisibility(View.GONE); 
    mProgressHUD.dismiss(); 
    super.onPostExecute(result); 
    showHTTPResponseMessage(statusCode); 
    } 

    protected void onProgressUpdate(Integer... progress) { 
    // pb.setProgress(progress[0]); 
    mProgressHUD.setMessage("Wait"); 
    super.onProgressUpdate(progress); 
    } 

    public JSONArray getData(String valueIWantToSend) throws JSONException, 
    ClientProtocolException, IOException, URISyntaxException { 

    URI url = new URI("http://10.10.9.101:9393/users"); 
    String result = ""; 

    int count = 20; 
    long totalSize = 0; 
    for (int i = 0; i < count; i++) { 
     publishProgress((Integer) ((i/count) * 100)); 
     // Escape early if cancel() is called 
     if (isCancelled()) break; 
    } 

    // create HttpClient 
    HttpClient httpclient = new DefaultHttpClient(); 

    // make GET request to the given URL 
    HttpResponse httpResponse = httpclient.execute(new HttpGet(url)); 

    // receive response as inputStream 
    InputStream inputStream = httpResponse.getEntity().getContent(); 

    // convert inputstream to string 
    if (inputStream != null) { 
     result = convertInputStreamToString(inputStream); 
     Log.i("Result >>>>> ", result); 
    } else 
     result = "Did not work!"; 

    StatusLine statusLine = httpResponse.getStatusLine(); 
    statusCode = statusLine.getStatusCode(); 
    Log.e("TAG", "HTTP Status Code: " + statusLine.getStatusCode()); 
    JSONArray jsonObject = new JSONArray(result.toString()); 
    return jsonObject; 
    } 

    @Override 
    public void onCancel(DialogInterface arg0) { 
    this.cancel(true); 
    mProgressHUD.dismiss(); 
    } 

    public void showHTTPResponseMessage(int messageCode) { 
    if (messageCode == 200) { 
     Toast.makeText(activity, "200 OOK Post Successfull", Toast.LENGTH_LONG) 
     .show(); 
    } 
    if (messageCode == 404) { 
     Toast.makeText(activity, "404 Not Found. Check your posting server url", 
      Toast.LENGTH_LONG).show(); 
    } 
    if (messageCode == 500) { 
     Toast.makeText(activity, 
      "500 Internal Server Error. Sorry We will back Soon!!", 
      Toast.LENGTH_LONG).show(); 
    } else if (messageCode == 0) 
     Toast.makeText(activity, "WOOOOOOFFFF... Unknonwn error", 
      Toast.LENGTH_LONG).show(); 
    } 

    private static String convertInputStreamToString(InputStream inputStream) 
     throws IOException { 
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
     inputStream)); 
    String line = ""; 
    String result = ""; 
    while ((line = bufferedReader.readLine()) != null) 
     result += line; 

    inputStream.close(); 
    return result; 

    } 

} 

도시하지 회를 제외하고 잘 작동하고 모든 일을로 구현, 왜?

답변

0

response = asynTask.get();을 사용하지 마십시오. 아직 응답을 기다리고 있기 때문에 창을 연결할 수 없습니다.

get()을 사용하는 대신 응답을 기반으로 작업을 수행하는 onPostExecute()에서 다른 메서드를 호출 할 수 있습니다.

+0

MainActivity에서 하나의 메소드를 작성하여 doInBackground에서 호출했습니다. MainActivity에서 JSON 응답을 저장하지만 실행 가능한 솔루션인가? –