我从服务器获得以下响应代码:
07-05 10:55:20.478 24688-24804/com.example.phuluso.aafs I/System.out: Response 400
以下是我尝试发布到Json
的{{1}}数据:
WCF server
这是我的android代码:
{
"Title":"Mrs",
"Name":"Amber",
"Surname":"Rose",
"Email":"amber@gmail.com",
"AuthenticationLevel":"S",
"ContactNumber":"0820653887",
"Password":"123",
"Gender":"Female",
"FundingType":"NSFAS",
"CampusId":2,"StudentNumber":201431511
}
这是我的wcf代码:
String jsonString = "";
try {
JSONStringer jsonStringer = new JSONStringer()
.object()
.key("Title").value("Mrs")
.key("Name").value("Amber")
.key("Surname").value("Rose")
.key("Email").value("amber@gmail.com")
.key("AuthenticationLevel").value("S")
.key("ContactNumber").value("0820653887")
.key("Password").value("123")
.key("Gender").value("Female")
.key("FundingType").value("NSFAS")
.key("CampusId").value(2)
.key("StudentNumber").value(201431511)
.endObject();
jsonString = jsonStringer.toString();
} catch (JSONException e) {
e.printStackTrace();
}
String http = "http://10.0.2.2:8750/WCF/UserRegistration.svc/registerStudentJson";
HttpURLConnection connection = null;
try {
System.out.println("Connecting to server");
URL url = new URL(http);
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept","application/json");
connection.setRequestProperty("Content-Type","application/json");
connection.setRequestProperty("charset", "UTF-8");
connection.setRequestProperty("Content-Length","352");
connection.setUseCaches(false);
connection.setConnectTimeout(50000);
connection.setReadTimeout(50000);
connection.connect();
System.out.println("Connected");
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
// out.write(studjason.toString());
out.write(URLEncoder.encode(jsonString.toString(),"UTF-8"));
out.flush();
out.close();
int result = connection.getResponseCode();
System.out.println("Response" + " " + result);
// System.out.println(studjason.toString());
System.out.println(jsonString.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
}
return null;
实现:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/registerStudentJson", ResponseFormat =
WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
void RegisterStudentJson(string student);