当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Http Response code 400 from android client

问题描述:

I Am getting the following response code from my server:

07-05 10:55:20.478 24688-24804/com.example.phuluso.aafs I/System.out: Response 400

The following is the Json data am trying to post to my WCF server :

{

"Title":"Mrs",

"Name":"Amber",

"Surname":"Rose",

"Email":"[email protected]",

"AuthenticationLevel":"S",

"ContactNumber":"0820653887",

"Password":"123",

"Gender":"Female",

"FundingType":"NSFAS",

"CampusId":2,"StudentNumber":201431511

}

Here is my android code:

String jsonString = "";

try {

JSONStringer jsonStringer = new JSONStringer()

.object()

.key("Title").value("Mrs")

.key("Name").value("Amber")

.key("Surname").value("Rose")

.key("Email").value("[email protected]")

.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;

And here is my wcf code:

[OperationContract]

[WebInvoke(Method = "POST", UriTemplate = "/registerStudentJson", ResponseFormat =

WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]

void RegisterStudentJson(string student);

Implementation:

public void RegisterStudentJson(string student)

{

JavaScriptSerializer oJS = new JavaScriptSerializer();

Student studentObject = new Student();

studentObject = oJS.Deserialize<Student>(student);

RegisterStudent(studentObject);

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: