2015-11-09 135 views
0

如何顯示警告消息,如果我無法與url進行握手,則顯示警報消息 我在下面的字段中傳遞URL,如果我無法見面該網址(404網址爲gatway錯誤)。我需要diplay一個警告框如果無法訪問服務器(url)

忘記進入onPOST等EXCUTE,它會停留在做後臺只,但如何顯示在待辦事項警報的背景 - 我的代碼

private class CheckTypesTask extends AsyncTask<Void, Void, Void> { 
     ProgressDialog asyncDialog = new ProgressDialog(MainActivity.this); 


     @Override 
     protected void onPreExecute() { 
      // set message of the dialog 
      asyncDialog.setMessage("Please Wait..."); 
      // show dialog 
      asyncDialog.setCancelable(false); 
      asyncDialog.show(); 
      super.onPreExecute(); 
     } 
      @Override 
     protected Void doInBackground(Void... arg0) { 


      try { 

       userna = user.getText().toString(); 
       passwd = pass.getText().toString(); 

       String serial = Build.SERIAL; 
       Log.e("login method", userna + " " + passwd + "" + serial); 
       ; 
       SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
       PropertyInfo propInfo1 = new PropertyInfo(); 
       propInfo1.setName("conidential"); 
       propInfo1.setValue(user.getText().toString().trim()); 
       propInfo1.setType(String.class); 
       request.addProperty(propInfo1); 

       PropertyInfo propInfo2 = new PropertyInfo(); 
       propInfo2.setName("conidential"); 
       propInfo2.setValue(pass.getText().toString().trim()); 
       propInfo2.setType(String.class); 
       request.addProperty(propInfo2); 

       PropertyInfo propInfo3 = new PropertyInfo(); 
       propInfo3.setName("DeviceId"); 
       propInfo3.setValue(serial); 
       propInfo3.setType(String.class); 
       request.addProperty(propInfo3); 

       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
         SoapEnvelope.VER11); 
       envelope.dotNet = true; 

       envelope.setOutputSoapObject(request); 
       HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
       androidHttpTransport.debug = true; 
       androidHttpTransport.call(SOAP_ACTION, envelope); 
       // Object response = (Object)envelope.getResponse(); 

       // SoapPrimitive response = 
       // (SoapPrimitive)envelope.getResponse(); 

       SoapObject response = (SoapObject) envelope.getResponse(); 

       String status = response.getProperty("Status").toString(); 

       if (status.equals("1")) { 

        storename = response.getProperty("StoreName").toString(); 
       } 

       Log.i("myApp", status.toString() + "Storename:" + storename); 
       typeStatus = status.toString(); 

      } catch (Exception e) { 
       e.printStackTrace(); 
       Log.e("exception at doin", e.toString()); 
      } 
      return null; 
     } 
       @Override 
     protected void onPostExecute(Void result) { 
      // hide the dialog 
      asyncDialog.dismiss(); 
      try { 
       if (typeStatus.equals("1")) { 
       ///////////doing my stuff 
      super.onPostExecute(result); 
      } 
     } 
     } 
     } 
+0

發佈錯誤堆棧跟蹤。 –

回答

0

你可嘗試將runOnUIthread放入doInBackground(Void ... arg0){

try { 
runOnUiThread(new Runnable() { 

        @Override 
        public void run() { 
         Toast.makeText(context, text, duration).show(); 
        } 
       }); 
       Thread.sleep(300); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      }