java - 从 Web 服务收到的响应为空

标签 java android eclipse web-services ksoap2

我正在使用 KSOAP 通过 Web 服务发送要存储在数据库中的详细信息。我使用 Visual Studio 来创建 Web 服务。网络服务运行良好。当详细信息插入数据库后,将返回一个字符串。问题是这个字符串是空的,也许我得到响应的方式有问题。我长期以来一直试图找出问题所在。请帮忙

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:58076/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();
             String result =  response.getProperty(0).toString();
             Log.d("res",result);
             if(result.equals("reg"))
             {
                 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(); ///This is what gets printed on screen
        }
    }

    @Override
    protected void onPreExecute() {
    }

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

    }   
  }

enter image description here

最佳答案

您的网络服务正在返回一个字符串。 尝试用它来解决您的问题

          Object result = envelope.getResponse();

当你的网络服务返回 byte[] 类型的值时,你可以这样做:

          SoapObject response=(SoapObject)envelope.bodyIn;

希望对你有帮助

关于java - 从 Web 服务收到的响应为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11427566/

相关文章:

java - 使用 void 方法修改对象或基元

android - 再次关于 SoundPool 的内存问题

android - 如何使用 Android 中的导航 Controller 以编程方式返回到以前的目的地?

java - eclipse 中的 R.menu 错误

使用 Code Assist (org.eclipse.mylyn.java.ui.javaAllCompletionProposalComputer) 时 Eclipse 非常慢

java - 从 2019 年 1 月开始,将从 Oracle Java 切换到 OpenJDK,以避免支付费用

java - 为什么Java迭代器构造函数签名没有迭代器类型?

java - 线程 "main"java.lang.ClassCastException : [Ljava. lang.Object 中的异常;不能转换为 model.AdminPopulate

android - 使用具有多个 src/res 文件夹的 android 库项目?

eclipse - 如何通过 svn Eclipse 插件解决树冲突