我使用volley将JSON对象从android代码发送到C#webservice。以下是Android代码:
List<JSONObject > items = new ArrayList<JSONObject>();
JSONObject field = new JSONObject();
String base64String = Base64.encodeToString(image_in_byte, Base64.DEFAULT);
String file_str = file.getName();
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SS");
String dateFormat = sdf.format(date);
field.put("ID", file_str);
field.put("IsTemp", "true");
field.put("Image", base64String);
field.put("Type", "JPEG" );
items.add(field);
JSONObject temp = new JSONObject();
temp.put("field1", "data1");
temp.put("field2", "data2");
temp.put("Input1", items);
temp.put("field3", dateFormat);
temp.put("field4", "test");
temp.put("field5", dateFormat);
temp.put("field6", "test");
try
{
String SERVER = "http:// webservice url";
JsonObjectRequest jsObjRequest = new
JsonObjectRequest(Request.Method.POST,SERVER,temp,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
System.out.println(response);
//hideProgressDialog();
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Toast.makeText(getApplicationContext(),
error.toString(), Toast.LENGTH_SHORT).show();
}
});
queue.add(jsObjRequest);
}
catch(Exception ex)
{
ex.printStackTrace();
}
以下是C#webservice
[AllowAnonymous]
[Route("webservice")]
[HttpPost]
public async Task<IHttpActionResult> UploadData(Data F_Data)
{
if (F_Data == null ||F_Data.Input1 == null)
{
return BadRequest("Input parameter is empty");
}
}
public class Input_F
{
public int ID { get; set; }
public bool IsTemp { get; set; }
public string Image { get; set; }
public string Type { get; set; }
}
public class Data
{
public string field1 { get; set; }
public string field2 { get; set; }
public List<Input_F> Input1{ get; set; }
public DateTime field3 { get; set; }
public string field4 { get; set; }
public DateTime field5{ get; set; }
public string field6 { get; set; }
}
在UploadData例程中,我将F_Data.Input1作为null。我能够获得Data类的其余字段,但是Input1为null。我不确定我是否正确地从我的Android代码发送Input1,任何指针都会有很大的帮助。