连接更改时奇怪的应用程序行为

时间:2012-06-27 04:58:30

标签: android web-services

我有一个以登录页面启动的应用程序,现在当用户在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...");

        }

    }

}

什么可能导致这种行为? 任何帮助将不胜感激..

1 个答案:

答案 0 :(得分:0)

通常,明确指定连接和数据接收的超时通常是个好主意,因为默认超时没有限制,当应用程序因网络连接而发生变化时,您的应用程序会永远等待(或很长时间)。

请尝试设置超时并告诉您是否看到任何结果?