我是 android 新手,我有一个简单的 android 应用程序可以收集信息并保存到 db。我想将此信息保存在我主机的 sql 服务器中。我将信息发布在 aSync 任务中,如下所示:
@Override
protected JSONObject doInBackground(String... arg0) {
// TODO Auto-generated method stub
// POST request to <service>/SaveVehicle
HttpPost request = new HttpPost(url);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
// Build JSON string
StringEntity entity;
try {
//Gson gson=new Gson();
//String jstr = gson.toJson(jsonObjSend);
//Log.d("json", jstr);
entity = new StringEntity(jsonObjSend.toString(),"UTF-8");
entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
entity.setContentType("application/json");
//InputStream content = entity.getContent();
request.setEntity(entity);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response;
try {
response = httpClient.execute(request);
Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
我的 wcf Iservice 看起来像这样:
[WebInvoke(
Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "CustInfo")]
[OperationContract]
ResponseCustInfo CustInfo(RequestCustInfo CustInfo);
[Serializable, DataContract(Name = "CustInfo")]
public class RequestCustInfo
{
[DataMember(Name = "StoreName")]
public string StoreName
{
get;
set;
}
[DataMember(Name="City")]
public string City
{
get;
set;
}
[DataMember(Name = "brands")]
public List<RequestCustBrands> brands
{
get;
set;
}
}
//service :
public ResponseCustInfo CustInfo(RequestCustInfo CustInfo)
{
ResponseCustInfo res = new ResponseCustInfo();
try
{
Android.Sync.CustInfo CustDeserializedObj = new Android.Sync.CustInfo();
if (CustInfo != null)
{
CustDeserializedObj.StoreName = CustInfo.StoreName;
CustDeserializedObj.City = CustInfo.City;
CustDeserializedObj.brands = CustInfo.brands;
}
//save object to sql...
而且我的 json 字符串是这样的:
{"CustInfo":[{"StoreName":"test","City":"تهران"},[{"CustomerRef":"25","BrandRef":"272","GoodsRef":"21"},{"CustomerRef":"25","BrandRef":"272","GoodsRef":"21"}]]}
我的问题是当我调试服务时,“CustInfo”对象为空。
我提前感谢您的帮助。