我有一個接收自定義類作爲輸入參數的'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是解決方案。 –