2012-12-13 2 views
0

웹 서비스 요청의 응답을 기다리는 동안 진행률 표시 줄을 표시하려고합니다. 그러나이 시간 동안 안드로이드 진행률 막대가로드되지 않습니다.android에서 웹 서비스를 연결하는 동안 진행률 표시 줄 표시

public class WebService extends Activity { 

      private static final String NAMESPACE="http://tempuri.org/"; 
      private static final String METHOD_NAME="AddEmployee"; 
      private static final String URL="http://10.32.4.24/Android/AndroidBus.svc"; 
      private static final String SOAP_ACTION="http://tempuri.org/IAndroidBus/AddEmployee"; 

      String celsius; 
      Button b; 
      TextView tv; 
      EditText et; 
      String res,resultval; 

      @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_web_service); 

      et=(EditText)findViewById(R.id.editText1);   
      tv=(TextView)findViewById(R.id.Result); 
      b=(Button)findViewById(R.id.button1); 
      b.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        new service().execute(); 
       } 
      }); 
    } 

    private class service extends AsyncTask<Void, Void, String> { 
     ProgressDialog pd; 
     protected void onPreExecute(){ 
      pd=new ProgressDialog(getBaseContext()); 
      pd.show(); 
     } 
     @Override 
     protected String doInBackground(Void... arg0) { 
      System.out.println("In DoIn Background"); 

      // Initialize soap request + add parameters 
      SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

      PropertyInfo pi=new PropertyInfo(); 
      pi.setName("Name"); 
      pi.setValue(et.getText().toString()); 
      request.addProperty(pi); 

         // Declare the version of the SOAP request 
      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
      envelope.setOutputSoapObject(request); 
      envelope.dotNet = true; 
      setProgress(1); 

      try { 
       HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

       // this is the actual part that will call the webservice 
       androidHttpTransport.debug=true; 
       androidHttpTransport.call(SOAP_ACTION, envelope); 

       String resdump=androidHttpTransport.responseDump.toString(); 
       System.out.println(resdump); 
       setProgress(2); 
       // Get the SoapResult from the envelope body. 
       //SoapObject result = (SoapObject) envelope.bodyIn; 
       SoapPrimitive result=(SoapPrimitive)envelope.getResponse(); 
       setProgress(3); 
       if (result != null) { 
        // Get the first property and change the label text 
        // txtFar.setText(result.getProperty(0).toString()); 
        res = result.toString(); 
       } else { 
        Toast.makeText(getApplicationContext(), "No Response", 
          Toast.LENGTH_LONG).show(); 
       } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 

      return res; 

     } 

     protected void onPostExecute(String h) { 
      String result = h; 
      pd.dismiss(); 
      tv.setText(result + "°F"); 

     } 

    } 


} 

요청/응답을 보내고받는 동안 진행률 표시 줄을 표시하려고합니다. 당신은 여기에 다른 컨텍스트 객체를 시도해야

protected void onPreExecute(){ 
     pd=new ProgressDialog(WebService.this); 
     pd.setMessage("Loading..."); 
     pd.setIndeterminate(true); 
     pd.show(); 
    } 

답변

0

봅니다 .. 대신의

,

ProgressDialog(getBaseContext()); 

ProgressDialog(ActivityName.this); 
시도
0

진행 대화 상자에 메시지를 설정하는

+0

유 예를 들어 작업을 줄 수 –

+0

PD = 새해서 ProgressDialog (WebService.this); – Nermeen

+0

네 누누가 제안한 것과 똑같습니다. ACityName을이 WebService로 바꿉니다.이 –

0

아닌가 setProgress 호출이 무엇을 알고 있지만, 그것이 진행 대화 상자를 업데이트한다고 가정, 당신은 당신의 AsyncTask를이 doInBackground 대신 setProgress

의 이것은에 publishProgress(2);

protected void onProgressUpdate(Integer... progress) { 
    setProgress(progress); 
} 

를 구현하고 호출 할 수 있도록해야 당신 때문에 다른 스레드에서 실행되는 doInBackGround 메서드에서 UI 요소를 업데이트 할 수 없습니다. 그렇게하면 대화 상자를 업데이트하지 않을뿐만 아니라 응용 프로그램을 중단시킬 가능성이 있습니다.

0

네 단계, 여기에 우리가 갈 ...

(1). 활동에 declear 진행 대화 상자

private ProgressDialog dialog = null; 

(2). 당신이 AsyncTask를 클래스

dialog = ProgressDialog.show(CurrentActivity.this, "", "loading.."); 

예를 시작할 때 대화를 시작 .

dialog = ProgressDialog.show(CurrentActivity.this, "", "loading search.."); 

SearchTask task= new GetSearchSeedsData(); 
task.execute(urlString); 

(3). AsyncTask 클래스의 doInBackground() 메소드에서 무거운 작업 (웹 서비스 또는 기타)을

(4)과 같이 수행하십시오. 는 당신이 뭘 하려는지

dialog.dismiss(); 
+0

진행률 표시 줄 요청 RDC 진행률 대화 상자가 표시되지 않습니다. –

+0

이 [자습서] (http://www.technotalkative.com/android-asynchronous-image-loading-in-listview/)를 확인하십시오. 친애하는 도움 – swiftBoy

1

이 잘못 AsyncTask를 클래스의 onPostExecute에서 진행 대화 상자를() 기각. SoapSerializationEnvelope, HttpTransportSE 및 Result 사이의 진행 설정은 HttpTransportSE.call (...) 내부에서 거대한 작업이 이루어지기 때문에 의미가 없습니다. 받은 바이트 수에 대한 다운로드/업로드 진행률 표시 줄을 실제로 보내려면 HttpTrasportSE 클래스를 수정해야합니다. 자세하게이 클래스의 call() 및 read()를 수정해야합니다.

당신은 예를 들어, 업로드 진행을 구현하기위한,이 부분, 당신의 HttpTransportSE를 만드는 모든 원본 코드를 복사하여 수정해야, here (HttpTransportSE.java) 볼 수 있듯이 : 나는 일반적인해서 ProgressDialog 고려했습니다 여기에

public List call(String soapAction, SoapEnvelope envelope, List headers, File outputFile) throws IOException, XmlPullParserException { 
(...) 
OutputStream os = connection.openOutputStream(); 
os.write(requestData, 0, requestData.length); 
os.flush(); 
os.close(); 
(...) 

을 () :

public List call(String soapAction, SoapEnvelope envelope, List headers, File outputFile, ProgressDialog dialog) 
    throws IOException, XmlPullParserException { 
    (...) 

    if(dialog != null) 
     { 
      dialog.setIndeterminate(false); 
      dialog.setProgress(0); 

      InputStream iss = new ByteArrayInputStream(requestData, 0, requestData.length); 

      int byteCount = 0; 
      byte[] buf = new byte[256]; 
      while (true) 
      { 
       int rd = iss.read(buf, 0, 256); 
       byteCount += rd; 
       if (rd == -1) 
        break; 
       os.write(buf, 0, rd); 

       dialog.setProgress((int) (((float)byteCount/(float)requestData.length)*100)); 
      } 
      dialog.setIndeterminate(true); 

      iss.close(); 
      buf = null; 
     } 
     else 
      os.write(requestData, 0, requestData.length); 

     os.flush(); 
     os.close(); 
(...) 

당신은 당신이 '널 (null)', 기본 논리를 통과 할 경우 (해서 ProgressDialog 또는 ProgressBar의 형이 당신의 목적에 따라)와 (그것에게 당신의 대화/줄을 통과하는 새로운 매개 변수를 추가 호출 메소드를 오버라이드 (override) 할 필요가 볼 수 있듯이 고려). 진행률 표시 줄에 실제 진행 상황 (ksoap2 기준)이 표시되도록하려면이 방법이 필요합니다.

다운로드 할 때 논리는 동일합니다. 다운로드가 관리되는 코드 부분을 찾으십시오. 응답 헤더에서 읽을 수있는 Content-Length 값을 저장하고 이전에 저장 한 내용 길이를 고려하여 바이트를 읽는 것과 유사한 업로드 "while"주기를 구현하십시오.

희망이 설명이 도움이 될 수 있습니다 당신

관련 문제