2012-07-11 67 views
0

我正在使用KSOAP发送使用Web服务存储在数据库中的详细信息。Web服务没有问题,它工作正常。此代码工作时,我没有用户AsyncTask类。我是新来的android和这是我第一次尝试使用AsyncTask类,它不工作。我附加了日志猫错误,doinbackround方法有些问题。我究竟做错了什么?请帮助执行doInBackround方法时发生错误

public class Registration extends Activity{ 
private static final String SOAP_ACTION = "http://tempuri.org/register"; 
private static final String OPERATION_NAME = "register"; 
private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/"; 
private static final String SOAP_ADDRESS = "http://10.0.2.2:54714/WebSite1/Service.asmx"; 
Button sqlRegister, sqlView; 

EditText sqlFirstName,sqlLastName,sqlEmail,sqlMobileNumber,sqlCurrentLocation,sqlUsername,sqlPassword; 

@Override 
protected void onCreate(Bundle savedInstanceState){ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.registration); 
sqlFirstName = (EditText) findViewById(R.id.etFname); 
sqlLastName = (EditText) findViewById(R.id.etLname); 
sqlEmail = (EditText) findViewById(R.id.etEmail); 
sqlMobileNumber = (EditText) findViewById(R.id.etPhone); 
sqlCurrentLocation = (EditText) findViewById(R.id.etCurrentLoc); 

sqlUsername = (EditText) findViewById(R.id.etUsername); 
sqlPassword = (EditText) findViewById(R.id.etPwd); 

sqlRegister = (Button) findViewById(R.id.bRegister); 
sqlRegister.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View v) { 
     switch (v.getId()){ 
     case R.id.bRegister: 
     new LongOperation().execute(""); 
     break; 
     } 
    } 
    }); 
} 

private class LongOperation extends AsyncTask<String, Void, String> { 

    @Override 
    protected String doInBackground(String... params) { 
     String firstname = sqlFirstName.getText().toString(); 
     String lastname = sqlLastName.getText().toString(); 
     String emailadd = sqlEmail.getText().toString(); 
     String number = sqlMobileNumber.getText().toString(); 
     String loc = sqlCurrentLocation.getText().toString(); 
     String uname = sqlUsername.getText().toString(); 
     String pwd = sqlPassword.getText().toString(); 

     SoapObject Request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME); 
     Request.addProperty("fname", String.valueOf(firstname)); 
     Request.addProperty("lname", String.valueOf(lastname)); 
     Request.addProperty("email", String.valueOf(emailadd)); 
     Request.addProperty("num", String.valueOf(number)); 
     Request.addProperty("loc", String.valueOf(loc)); 
     Request.addProperty("username", String.valueOf(uname)); 
     Request.addProperty("password", String.valueOf(pwd)); 
     Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show(); 

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet = true; 
     envelope.setOutputSoapObject(Request); 
     HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS); 
     try 
     { 
      httpTransport.call(SOAP_ACTION, envelope); 
      SoapObject response = (SoapObject)envelope.getResponse(); 
      int result = Integer.parseInt(response.getProperty(0).toString()); 
      if(result == '1') 
      { 
       return "Registered"; 
      } 
      else 
      { 
       return "Not Registered"; 
      } 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
     return null; 

    }  

    @Override 
    protected void onPostExecute(String result) { 
     if(result=="Registered") 
     { 
     Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show(); 
     } 
     else if(result =="Not Registered") 
     { 
     Toast.makeText(Registration.this, "Try Again", Toast.LENGTH_LONG).show(); 
     } 
     else 
     { 
      Toast.makeText(Registration.this, "Somethings wrong", Toast.LENGTH_LONG).show(); 
     } 
    } 

    @Override 
    protected void onPreExecute() { 
    } 

    @Override 
    protected void onProgressUpdate(Void... values) { 
    } 
    } 
} 

enter image description here

////编辑

public class Registration extends Activity{ 
private static final String SOAP_ACTION = "http://tempuri.org/register"; 
private static final String OPERATION_NAME = "register"; 
private static final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/"; 
private static final String SOAP_ADDRESS = "http://10.0.2.2:54714/WebSite1/Service.asmx"; 
Button sqlRegister, sqlView; 

EditText sqlFirstName,sqlLastName,sqlEmail,sqlMobileNumber,sqlCurrentLocation,sqlUsername,sqlPassword; 

@Override 
protected void onCreate(Bundle savedInstanceState){ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.registration); 
sqlFirstName = (EditText) findViewById(R.id.etFname); 
sqlLastName = (EditText) findViewById(R.id.etLname); 
sqlEmail = (EditText) findViewById(R.id.etEmail); 
sqlMobileNumber = (EditText) findViewById(R.id.etPhone); 
sqlCurrentLocation = (EditText) findViewById(R.id.etCurrentLoc); 

sqlUsername = (EditText) findViewById(R.id.etUsername); 
sqlPassword = (EditText) findViewById(R.id.etPwd); 

sqlRegister = (Button) findViewById(R.id.bRegister); 
sqlRegister.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View v) { 
     switch (v.getId()){ 
     case R.id.bRegister: 
     new LongOperation().execute(""); 
     break; 
     } 
    } 
    }); 
} 

private class LongOperation extends AsyncTask<String, Void, String> { 

    @Override 
    protected String doInBackground(String... params) { 
     String firstname = sqlFirstName.getText().toString(); 
     String lastname = sqlLastName.getText().toString(); 
     String emailadd = sqlEmail.getText().toString(); 
     String number = sqlMobileNumber.getText().toString(); 
     String loc = sqlCurrentLocation.getText().toString(); 
     String uname = sqlUsername.getText().toString(); 
     String pwd = sqlPassword.getText().toString(); 

     SoapObject Request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME); 
     Request.addProperty("fname", String.valueOf(firstname)); 
     Request.addProperty("lname", String.valueOf(lastname)); 
     Request.addProperty("email", String.valueOf(emailadd)); 
     Request.addProperty("num", String.valueOf(number)); 
     Request.addProperty("loc", String.valueOf(loc)); 
     Request.addProperty("username", String.valueOf(uname)); 
     Request.addProperty("password", String.valueOf(pwd)); 

     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
     envelope.dotNet = true; 
     envelope.setOutputSoapObject(Request); 
     HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS); 
     Log.d("work","work"); 
     try 
     { 
      httpTransport.call(SOAP_ACTION, envelope); 
      SoapObject response = (SoapObject)envelope.getResponse(); 
      int result = Integer.parseInt(response.getProperty(0).toString()); 
      if(result == 1) 
      { 
       Log.d("reg","reg"); 
       return "Registered"; 
      } 
      else 
      { 
       Log.d("no","no"); 
       return "Not Registered"; 
      } 
     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
     return null; 

    }  

    @Override 
    protected void onPostExecute(String result) { 
     Log.d("tag","onpost"); 
     if(result!=null) 
     { 

      if(result.equals("Registered")) 
       { 
        Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show(); 
       } 
      else if(result.equals("Not Registered")) 
       { 
        Toast.makeText(Registration.this, "Try Again", Toast.LENGTH_LONG).show(); 
       } 
     } 
     else 
     { 
      Toast.makeText(Registration.this, "Somethings wrong", Toast.LENGTH_LONG).show(); 
     } 
    } 

    @Override 
    protected void onPreExecute() { 
    } 

    @Override 
    protected void onProgressUpdate(Void... values) { 
    } 

    } 

}

+0

其仍然没有工作,对不对? – 2012-07-11 05:53:02

+0

你不工作:(只有日志消息工作正在显示日志猫 – 2012-07-11 06:04:21

+0

尝试记录什么字符串返回,并告诉我们 – 2012-07-11 06:07:24

回答

0

试试这个....

  1. 始终保持UI工作UI线程和非UI在非UI线程中工作。

  2. doInBackground是一个非UI线程,所以你不能在这个UI线程 上发布任何东西。

  3. Toast语句是doInBackgroud中的问题,将其移至postExecute,即在UI线程上工作的 。

    Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show(); 
    
+0

谢谢我从doinbackround()删除吐司现在我没有得到任何错误,但问题是没有发生。没有显示敬酒 – 2012-07-11 04:59:31

+0

您在doPost中宣布的敬酒是否显示? – 2012-07-11 05:09:04

+0

等待doInBackground中的任务完成,看到敬酒。保留Log语句以了解最新情况 – 2012-07-11 05:11:22

0

你打电话

Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show();

内部doInBackground(),这不是在UI线程中运行。导致问题。

试试这个:

runOnUiThread(new Runnable() { 
    public void run() { 
     Toast.makeText(Registration.this, "You have been registered Successfully", Toast.LENGTH_LONG).show(); 
    } 
}); 
} 

或移动Toast.makeText到在UI线程

+0

我从doInBackground()中删除了Toast。我没有收到任何错误,但是当我点击注册按钮时没有任何反应。没有一个吐司会显示:( – 2012-07-11 04:50:41

+0

如果你把它移走了,你怎么能显示吐司?而且你不需要移除它。试试我上面发布的代码。在runOnUiThread中运行'Toast.makeText'。它应该可以工作。 – user1417127 2012-07-11 05:34:11

0

运行。这是因为您呼叫吐司在doInBackground()onPostExecute()方法。无论doInBackground中的UI如何,都会造成looper()异常。所以尽量让在PostExecute(用户界面)的AsynTask 作为

@Override 
protected void onPostExecute(String result) { 

Toast.makeText(Registration.this, "You have been registered Successfully",Toast.LENGTH_LONG).show(); 

} 
} 

它可能对你有帮助..

0

你不应该使用==比较字符串。

protected void onPostExecute(String result) { 
     if(result=="Registered") 

这可能是意外行为背后的原因。尝试使用类似

result.equals("registered") 

阅读this

+0

除了'doInBackground()'中的面包外,当然还有 – 2012-07-11 05:07:59

+0

更改了两个仍然没有工作:/没有烤面包被显示。检查我的编辑 – 2012-07-11 05:36:31

+0

使用日志或控制台输出来检查你的'onPostExecute()'是否被调用。 – 2012-07-11 05:38:28

相关问题