获得 POST 结果 JSON
Get POST Result in JSON
我正在使用 POST 发送数据。我得到 JSON 的答案。
如果我使用纯文本而不是 JSON,它可以工作,但它不适用于 JSON。
我在应用程序中没有收到任何错误屏幕
if (status) = true -> 注册成功
导入:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import javax.xml.transform.Result;
JSON:
[{"status":true,"result":"registerSuccessful","message":"Register succeeded"}]
JAVA代码:
private void registernewuser() {
StringRequest request = new StringRequest(Request.Method.POST, registerUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
String RegisterResultString = jsonResponse.getString("status");
if (RegisterResultString.equals("true")) {
Toast.makeText(getApplicationContext(), "Successfully Registered User", Toast.LENGTH_LONG).show();
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "Sorry, Error Registering New User", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("registeruser", "true");
params.put("username", username.getText().toString().trim());
params.put("userpassword", userpassword.getText().toString().trim());
params.put("useremail", useremail.getText().toString());
return params;
}
};
Volley.newRequestQueue(this).add(request);
}
我在构建应用程序时没有遇到问题。但我找不到我的错。
您需要在请求中添加 header 以表明 JSON 用法:
request.addHeader("content-type", "application/json");
否则它将解释为纯文本。
你的 JSON 代码是一个 JSONArray
和一个 JSONObject
因为方括号。因此,只需将 jsonResponse
的类型设置为 JSONArray
并使用 JSONArray.getJSONObject(0)
方法获取 JSONObject
。
JSONArray jsonResponse = new JSONArray(response);
String registerResultString = jsonResponse.getJSONObject(0).getString("status");
您可以将请求调用为 JSONRequest
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
your_url, new com.android.volley.Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
};
我正在使用 POST 发送数据。我得到 JSON 的答案。 如果我使用纯文本而不是 JSON,它可以工作,但它不适用于 JSON。
我在应用程序中没有收到任何错误屏幕
if (status) = true -> 注册成功
导入:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import javax.xml.transform.Result;
JSON:
[{"status":true,"result":"registerSuccessful","message":"Register succeeded"}]
JAVA代码:
private void registernewuser() {
StringRequest request = new StringRequest(Request.Method.POST, registerUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
String RegisterResultString = jsonResponse.getString("status");
if (RegisterResultString.equals("true")) {
Toast.makeText(getApplicationContext(), "Successfully Registered User", Toast.LENGTH_LONG).show();
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(), "Sorry, Error Registering New User", Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("registeruser", "true");
params.put("username", username.getText().toString().trim());
params.put("userpassword", userpassword.getText().toString().trim());
params.put("useremail", useremail.getText().toString());
return params;
}
};
Volley.newRequestQueue(this).add(request);
}
我在构建应用程序时没有遇到问题。但我找不到我的错。
您需要在请求中添加 header 以表明 JSON 用法:
request.addHeader("content-type", "application/json");
否则它将解释为纯文本。
你的 JSON 代码是一个 JSONArray
和一个 JSONObject
因为方括号。因此,只需将 jsonResponse
的类型设置为 JSONArray
并使用 JSONArray.getJSONObject(0)
方法获取 JSONObject
。
JSONArray jsonResponse = new JSONArray(response);
String registerResultString = jsonResponse.getJSONObject(0).getString("status");
您可以将请求调用为 JSONRequest
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
your_url, new com.android.volley.Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}, new com.android.volley.Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
};