使用 android 中的 KSOAP 库将 JSON 数组发送到 .net 服务器
Send JSON Array to .net server using KSOAP library in android
我想使用 KSOAP 库将 JSON 对象/JSON 数组发送到 .net 服务器。
这是我的代码
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();
}
}
它正在抛出无法序列化的异常
04-27 12:52:46.378: D/Response 来自服务器 (5982): java.lang.RuntimeException: 无法序列化: {"MacID":"1","Latitude":"18.5647613 ","Longitude":"73.8069672"}
提前致谢
那个:
pi.setType(JSONObject.class);
将不起作用,因为在 ksoap2 中没有为 JSONObject 定义标准序列化。但我认为你没有写的是你试图将 JSON 字符串发送到服务器。我是对的,那么你必须更改你的代码以发送字符串并使用 toString 这种方式将 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);
如果我错了,那么你必须将整个 JSON对象解析为 SoapObjects/Primitives 的结构。
此致,马尔辛
我想使用 KSOAP 库将 JSON 对象/JSON 数组发送到 .net 服务器。
这是我的代码
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();
}
}
它正在抛出无法序列化的异常 04-27 12:52:46.378: D/Response 来自服务器 (5982): java.lang.RuntimeException: 无法序列化: {"MacID":"1","Latitude":"18.5647613 ","Longitude":"73.8069672"}
提前致谢
那个:
pi.setType(JSONObject.class);
将不起作用,因为在 ksoap2 中没有为 JSONObject 定义标准序列化。但我认为你没有写的是你试图将 JSON 字符串发送到服务器。我是对的,那么你必须更改你的代码以发送字符串并使用 toString 这种方式将 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);
如果我错了,那么你必须将整个 JSON对象解析为 SoapObjects/Primitives 的结构。
此致,马尔辛