我有一个接收自定义类作为输入参数的'wcf'rest服务。我想通过android平台发送这个类的对象数据。我用httpUrlConnection
。我总是收到响应代码500.但服务在Windows窗体应用程序中正常工作。我在Android的代码是在这里:将json作为对象发送给wcf from android
HttpURLConnection urlConnection = null;
String jsonString = "";
JSONObject jsonObject = new JSONObject();
JSONObject jsonObject2 = new JSONObject();
try {
jsonObject.put("A","rtrt");
jsonObject2.put("c",jsonObject);
} catch (JSONException e) {
e.printStackTrace();
}
try{
URL url = new URL("http://.../Service1.svc/GetTestData");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("ContentType","applicaiton/json");
urlConnection.setUseCaches(false);
urlConnection.connect();
OutputStream outputStream = new BufferedOutputStream(urlConnection.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));
writer.write(jsonObject2.toString());
writer.flush();
writer.close();
outputStream.close();
int statusCode = urlConnection.getResponseCode();
// Log.d("TRAFFIC", "err: " + urlConnection.getErrorStream().toString());
这是不正确的。如果更改绑定并将其设置为webhttpbinding,则Wcf支持休息。 –
我是.Net以及Android开发人员,很久以前就面临这个问题,Ksoap2是解决方案。 –