我是编程世界的新手,所以我只需要一些帮助。 我必须在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个字符串。问题是:它除了&#39; anyType {}&#39;之外不会返回任何内容,而且我不确定如何接收两个字符串。我已经评论了两行,因为我不确定通过envelope.bodyIn或envelope.getResponse()来获取结果是否更好。
也许,发布收到结果的活动是必要的:
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 {})答案。
答案 0 :(得分:1)
它只返回一个空(anyType {})答案。
这意味着没有任何data
返回。
但是,如果您不想获得anyType{}
,而是empty string
:
而不是:
result.getProperty(0);
使用:
result.getPrimitivePropertyAsString(0);
以下是如何使用webservice
从KSOAP
获取数据的示例( 如果您期望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;
}