2012-06-27 48 views
1

我有一個應用程序,它啓動了一個登錄頁面,現在當用戶使用wifi時,它工作正常,但是當他/她切換到數據連接時,web服務不響應。 在此之後,當用戶手動強制停止/重新安裝它適用於數據連接/再次wifi的應用程序,但上述給出了同樣的行爲, 我的代碼如下:連接變化時奇怪的應用程序行爲

public class Logg extends Activity { 
    EditText log, pw, tfld; 
    Button bt; 
    String logg, pww, resp ; 
    static String employidd; 
    ProgressDialog p; 
    private static String SOAP_ACTION = "MyName/CheckLogin"; // "http://tempuri.org/CheckLogin"; 
    private static String NAMESPACE = "MyName"; // "http://tempuri.org/"; 
    private static String METHOD_NAME = "CheckLogin"; 
    private static String URL = "http://timetracker.e-pspl.com/erm-login/";  //"http://tdsw025.pcsolutions.com/ERMWeb/"; 

    /* Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     log = (EditText) findViewById(R.id.logger1); 
     pw = (EditText) findViewById(R.id.pwwrs); 
     bt = (Button) findViewById(R.id.subb); 
     bt.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       logg = log.getText().toString(); 
       pww = pw.getText().toString(); 
       if (logg.equals("")) { 
        Toast.makeText(Logg.this, "Login ID cannot be blank", 
          Toast.LENGTH_SHORT).show(); 
       } 
       if (pww.equals("")) { 
        Toast.makeText(Logg.this, "Password cannot be blank", 
          Toast.LENGTH_SHORT).show(); 
       } else { 

        try { 
         new SoapLog().execute(); 
        } catch (Exception e) { 
         Log.v("xcptn", e.toString()); 
        } 

       } 
      } 
     }); 
    } 

    private class SoapLog extends AsyncTask<Void, Void, Void> { 

     @Override 
     protected Void doInBackground(Void... params) { 
      // TODO Auto-generated method stub 
      // Initialize soap request + add parameters 
      SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

      // Use this to add parameters 
      request.addProperty("UserName", logg); 
      request.addProperty("Pwd", pww); 
      // Declare the version of the SOAP request 
      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
        SoapEnvelope.VER11); 
      envelope.dotNet = true; 
      envelope.setOutputSoapObject(request); 
      Log.v("req", request + ""); 
      HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
      try { 
       // this is the actual part that will call the webservice 
       androidHttpTransport.call(SOAP_ACTION, envelope); 
       Log.v("envelope", envelope + ""); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      try{ 
       // Get the SoapResult from the envelope body. 
       SoapObject result = (SoapObject) envelope.bodyIn; 
       resp = result.toString(); 
       Log.v("respmain", resp); 
      }catch(NullPointerException e){ 
       e.printStackTrace(); 
      } 


      return null; 

     } 

     @Override 
     protected void onPostExecute(Void result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 
      p.dismiss(); 
      if (resp != null) { 

       // // if (resp.contains("true")) { 
       // Intent one = new Intent(Logg.this, Second.class); 
       // startActivity(one); 
       // finish(); 
       // // } 
       if (resp.contains("false")) { 
        Toast.makeText(Logg.this, "Invalid UserID/Password", 
          Toast.LENGTH_SHORT).show(); 
        // StringTokenizer tokens = new StringTokenizer(resp, "="); 
        // String first = tokens.nextToken(); 
        // String second = tokens.nextToken(); 
        // Log.v("tag", first); 
        // Log.v("tag1", second); 
       } else { 
        String str[] = resp.split("="); 
        String tempStr[] = str[1].split(";"); 
        employidd = tempStr[0]; 
        Log.v("id", employidd); 
        Toast.makeText(Logg.this, "Success!!", Toast.LENGTH_SHORT) 
          .show(); 
        Intent onne = new Intent(Logg.this, DateList.class); 
        onne.putExtra("ID", employidd); 
        startActivity(onne); 
        finish(); 
       } 
      } 
     } 

     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      super.onPreExecute(); 
      p = ProgressDialog.show(Logg.this, "", "Loading..."); 

     } 

    } 

} 

什麼可能會導致這行爲?? 任何幫助將不勝感激..

回答

0

通常這是一個好主意,明確指定連接和數據接收的超時,因爲默認超時沒有限制,並且您的應用程序一直等待(或相當長的時間)網絡連接。

請嘗試設置超時並告訴您是否看到任何結果?

+0

在我的大部分活動中,我使用的是httpget函數,當我對我的活動調用finish()時關閉它的最佳方式是什麼? –