【问题标题】:Webservice returns anyType{} androidWebservice 返回 anyType{} android
【发布时间】:2015-05-27 05:31:10
【问题描述】:

我是编程界的新手,所以我需要一些帮助。 我必须在 android 上使用 web 服务。我已经完成了一个可以正常工作的方法,但是当我必须传递参数时就会出现问题。我不确定我做错了什么。

AsyncTask 的代码如下:

public class PegaHorarioProfessorTask extends AsyncTask<Object, Object, String> {

    private static final String NAMESPACE = "http://tempuri.org/";
    private static String URL = "http://fatec.aied.com.br/publico.asmx?WSDL"; //"10.68.76.4/publico.asmx?WSDL";  //        http://fatec.aied.com.br/publico.asmx?WSDL
    private static final String SOAP_ACTION = "http://tempuri.org/HorarioProfessor";

        @Override
        public String doInBackground(Object... objects) {
            String frase = null;

            SoapObject soap = new SoapObject(NAMESPACE, "HorarioProfessor");

            String id = "11";

            PropertyInfo profId =new PropertyInfo();
            profId.setName("Id");
            profId.setValue(id);
            profId.setType(String.class);
            soap.addAttribute("Id", id);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(soap);
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

            SoapObject result = null;

            try {

                androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapObject resultado1 = (SoapObject)envelope.getResponse();

                //result = (SoapObject) envelope.bodyIn;
                //result.getProperty(0);

                if (result != null) {
                    Log.i("Result", result.toString());
                    return result.toString();
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            return result.toString();

        }
    }

所以,我传递了 ID 11,它返回 2 个字符串。问题是:除了“anyType{}”之外,它不返回任何内容,而且我不确定如何接收两个字符串。我评论了两行,因为我不确定通过信封.bodyIn 或信封.getResponse() 获得结果是否更好。

也许,需要发布接收结果的Activity:

public class ActivityHorarioProfessor extends ActionBarActivity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_horario_professor);

        String[] aux = null;

        Intent intent = this.getIntent();
        Professores professor = (Professores) intent.getSerializableExtra("professorSelecionado");
        if(professor!=null) {
            PegaHorarioProfessorTask prof = new PegaHorarioProfessorTask();
            Log.i("Número", String.valueOf(prof));
            String auxi = null;
            try {
                auxi = prof.execute().get().toString();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }

            Log.i("Auxi", String.valueOf(auxi));
        }
        else {
            Log.i("It's", "WRONG");
        }

    }
}

它只是返回一个空的 (anyType{}) 答案。

【问题讨论】:

  • 你能否请求json或xml格式的数据,这样编码更灵活,hrskrs已经解决了你的问题

标签: android getresponse envelope


【解决方案1】:

它只是返回一个空的 (anyType{}) 答案。

这意味着没有任何data返回。

但是,如果您不想获得 anyType{} 而是 empty string

代替:

result.getProperty(0);

使用:

result.getPrimitivePropertyAsString(0);

这是一个关于如何使用 KSOAPwebservice 获取数据的示例(如果您期望 String 作为 response):

public static String getDataAsString(){
    String responseString = "";
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty(KEY, value);
    SoapSerializationEnvelope envelope = getSoapSerializationEnvelope(request);
    HttpTransportSE httpTransportSE = getHttpTransportSE();
    try {
      httpTransportSE.call(ACTION_NAME, envelope);
      SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
      responseString = response.toString();

    } catch (XmlPullParserException e) {
      e.printStackTrace();
    } catch (SoapFault soapFault) {
      soapFault.printStackTrace();
    } catch (HttpResponseException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    return responseString;
}

getSoapSerializationEnvelope函数:

 public static final SoapSerializationEnvelope getSoapSerializationEnvelope(SoapObject request){
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.implicitTypes = true;
        envelope.setAddAdornments(false);
        envelope.setOutputSoapObject(request);

        return envelope;
    }

getHttpTransportSE函数:

 public static final HttpTransportSE getHttpTransportSE(){
        HttpTransportSE ht = new HttpTransportSE(URL);
        ht.debug = true;
        ht.setXmlVersionTag("<!--?xml version=\"1.0\" encoding= \"UTF-8\" ?-->");
        return ht;
    }

【讨论】:

  • 感谢您提供的示例,它会很有帮助。但问题是,当我在 SoapUI 上运行它时,发送 11,它返回两个字符串。通过代码,它返回一个空结果,而不是两个字符串。
  • @ViniciusCruz 然后检查您是否发送正确METHOD_NAME 并正确发送response。具体检查HorarioProfessorprofessorSelecionado
猜你喜欢
  • 2014-10-04
  • 1970-01-01
  • 2012-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多