我对帖子请求有疑问。服务器给我回复消息确定,但没有识别json我发送的帖子是什么。我几乎尝试了一切。 Php属性$ _POST始终为空。 谢谢你的回答。
Json有结构:
sizeof
Android代码:
{
"data": {
"email": "something@something.com",
"password": "tralala"
}
}
服务器响应:
public static Pair<Integer, String> signUpByEmailPost(String username, String passwd) {
try {
URL url = new URL(SERVER_URL + "create/createUser.php");
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
urlConn.setRequestMethod("POST");
urlConn.setRequestProperty("Content-Type", "application/json");
urlConn.setDoOutput(true);
urlConn.setDoInput(true);
urlConn.setUseCaches(false);
urlConn.connect();
JSONObject json = createJsonCredentials(username, passwd);
String dataString = json.toString();
Log.i(TAG, dataString);
OutputStreamWriter out = new OutputStreamWriter(urlConn.getOutputStream());
out.write(dataString);
out.flush();
int httpResult = resolveHttpResponseCode(urlConn);
if(httpResult > 0) {
return new Pair<>(httpResult, null);
}
String receivedDataString = getStringContentFromConnection(urlConn);
return new Pair<>(0, receivedDataString);
} catch (Exception e) {
e.printStackTrace();
}
return new Pair<>(3, null);
}
private static JSONObject createJsonCredentials(String username, String password) {
try {
JSONObject jsonInner = new JSONObject();
jsonInner.put("email", "something@something.com");
jsonInner.put("password", "tralala");
JSONObject jsonOuter = new JSONObject();
jsonOuter.put("data", jsonInner);
return jsonOuter;
} catch(JSONException ex) {
ex.printStackTrace();
}
return null;
}
服务器端:
{
success: false,
errors: {
email: "blank_email",
password: "blank_password"
}
}
答案 0 :(得分:0)
您好在这里从方法中返回null值。试试这个代码。它会起作用。
private static JSONObject createJsonCredentials(String username, String password) {
JSONObject jsonOuter = new JSONObject();
try {
JSONObject jsonInner = new JSONObject();
jsonInner.put("email", "something@something.com");
jsonInner.put("password", "tralala");
jsonOuter.put("data", jsonInner);
} catch(JSONException ex) {
ex.printStackTrace();
}
return jsonOuter;
}