我想使用KSOAP库将JSON对象/ JSON数组发送到.net Server。
这是我的代码
sendJSON{
JSONObject json = new JSONObject();
try {
CallingSoap cs=new CallingSoap();
String macid="1";
String latStr= StaticVariableClass.latitude_str;
String longStr= StaticVariableClass.longitude_str;
String datetimeStr="23/04/2015";
json.put("MacID",macid);
json.put("DateTime",datetimeStr);
json.put("Latitude",latStr);
json.put("Longitude",longStr );
String JSONString= json.toString();
Log.e("JSON", JSONString);
// String resp=cs.demo("test");
String resp=cs.demo(json); // I need to send this json to my asp.net web server
Log.d("Response from server",resp);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//CallingSoap .java
public class CallSoap {
public String demo(JSONObject a)
//public String demo(String a)
{
final String SOAP_ACTIONS= "http://tempuri.org/GetLatLongJson";
final String OPERATION_NAMES= "GetLatLongJson";
final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
final String SOAP_ADDRESS = "http://10.208.36.33/samtadoot2/samtadootwebservice.asmx";
SoapObject request=new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAMES);
PropertyInfo pi=new PropertyInfo();
pi.setName("jsonobject");
pi.setValue(a);
pi.setType(JSONObject.class);
request.addProperty(pi);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
new MarshalBase64().register(envelope);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
Object response=null;
try
{
httpTransport.call(SOAP_ACTIONS, envelope);
response = envelope.getResponse();
}
catch(Exception ex)
{
response=ex.toString();
}
//JSONArray JSONArray = null;
return response.toString();
}
}
它抛出一个不能seralize的例外 04-27 12:52:46.378:D /来自服务器的响应(5982):java.lang.RuntimeException:无法序列化:{“MacID”:“1”,“纬度”:“18.5647613”,“经度”:“73.8069672 “}
提前致谢
答案 0 :(得分:0)
的是:
pi.setType(JSONObject.class);
不起作用,因为在ksoap2中没有为JSONObject定义标准序列化。但是我赞成你写的不是你试图将JSON字符串发送到服务器。我是对的,那么你必须改变你的代码以使用toString以这种方式发送String并将JSONObject编码为JSON字符串:
SoapObject request=new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAMES);
PropertyInfo pi=new PropertyInfo();
pi.setName("jsonobject");
pi.setValue(a.toString());
pi.setType(String.class);
request.addProperty(pi);
如果我错了,那么你必须将整个JSONObject解析为SoapObjects / Primitives的结构。
此致,Marcin