我正在尝试在android中集成SOAP Web服务,我对肥皂网服务如何工作不够了解,在搜索到互联网后我已经在android studio中集成了kSOAP lib并编写了这段代码
public class SOAP extends AppCompatActivity {
String URL = "http://www.getrixi.com/GoTukxiWebServices/TukxiService.asmx";
String NAMESPACE = "http://tempuri.org/GetAssets";
String SOAP_ACTION = "http://tempuri.org/GetAssets";
String METHOD_NAME = "GetAssets";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_soap);
Button bt_id = (Button) findViewById(R.id.bt_id);
bt_id.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new CallWebService().execute();
}
});
}
class CallWebService extends AsyncTask<String, Void, String> {
@Override
protected void onPostExecute(String s) {
Log.e("TAG", "the result from server is: " + s);
}
@Override
protected String doInBackground(String... params) {
String result = "";
SoapObject soapObject = new SoapObject(NAMESPACE, METHOD_NAME);
soapObject.addProperty("Lat", "31.71");
soapObject.addProperty("Long", "74.34");
soapObject.addProperty("Radius", "30");
soapObject.addProperty("AssetType", "all");
/* PropertyInfo propertyInfo = new PropertyInfo();
propertyInfo.setName("Lat");
propertyInfo.setName("Long");
propertyInfo.setName("Radius");
propertyInfo.setName("AssetType");
propertyInfo.setValue(params[0]);
propertyInfo.setValue(params[1]);
propertyInfo.setValue(params[2]);
propertyInfo.setValue(params[3]);
propertyInfo.setType(String.class);
soapObject.addProperty(propertyInfo);*/
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(soapObject);
HttpTransportSE httpTransportSE = new HttpTransportSE(URL);
try {
httpTransportSE.call(SOAP_ACTION, envelope);
SoapPrimitive soapPrimitive = (SoapPrimitive)envelope.getResponse();
Log.e("TAG", "the message result : " + soapPrimitive);
result = soapPrimitive.toString();
Log.e("TAG", "the resul is: " + result);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
我怎样才能获得json结果 我在这里测试了api http://getrixi.com/GoTukxiWebServices/TukxiService.asmx?op=GetAssets 它使用get方法在web上正常工作 我怎样才能在android中获得结果
答案 0 :(得分:0)
这段代码可以更具说明性:
String str="http://your.domain.here/yourSubMethod";
URLConnection urlConn = null;
BufferedReader bufferedReader = null;
try
{
URL url = new URL(str);
urlConn = url.openConnection();
bufferedReader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
StringBuffer stringBuffer = new StringBuffer();
String line;
while ((line = bufferedReader.readLine()) != null)
{
stringBuffer.append(line);
}
return new JSONObject(stringBuffer.toString());
}
catch(Exception ex)
{
Log.e("App", "yourDataTask", ex);
return null;
}
有关详细信息,请阅读Android:Fetching JSONARRAY Data from Web api