【发布时间】:2018-06-11 05:07:07
【问题描述】:
我不确定我应该使用哪种类型的内容来发布到 api,因为我对这个发展中国家很陌生。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
button = (Button)findViewById(R.id.reg_btn_sign_up);
btnCancel = (Button)findViewById(R.id.reg_button_cancel);
Email = (EditText)findViewById(R.id.reg_email);
Name = (EditText)findViewById(R.id.reg_name);
Pass = (EditText)findViewById(R.id.reg_pass);
ConPass = (EditText)findViewById(R.id.reg_confirm_pass);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
email = Email.getText().toString();
name = Name.getText().toString();
password = Pass.getText().toString();
conPass = ConPass.getText().toString();
JSONObject jsonBody = new JSONObject();
try {
jsonBody.put("username", email);
jsonBody.put("password", password);
jsonBody.put("name", name);
} catch (JSONException e) {
e.printStackTrace();
}
final String mRequestBody = jsonBody.toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, reg_url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0);
String status = jsonObject.getString("status");
String result = jsonObject.getString("result");
builder.setTitle("Server Response...");
builder.setMessage(result);
} catch (JSONException e){
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
})
{
@Override
public byte[] getBody() throws AuthFailureError {
try {
return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
};
MySingleton.getmInstance(RegisterActivity.this).addRequestQueue(stringRequest);
}
});
}
}
这是我在 logcat 中得到的日志:
W/System.err: org.json.JSONException: 值 org.json.JSONObject 类型的 {"status":0,"result":"Access restricted"} 无法转换为 JSONArray
我在 POSTMAN 中正确执行此操作,但未能在 android 中获得我想要的结果。
在命令中添加 API。
【问题讨论】:
-
请求:$url/userRegister/?token=$token 方法:POST 请求:params = {"user_id":"","application_id":"device_id","username":"", "timestamp":"123456789","version":"app_version","language":"en or zh","platform":"ios or android"} data = {"username":"test@test.com" ,"password":"test","name":"test"} 响应:
-
您收到此错误是因为您尝试将 JSONObject 转换为 JSONArray
-
但这几乎都是我指的在线资源。 =(
-
您能否添加您收到的回复
-
我没有得到任何回应。我什至不知道我在做什么。计划重做。有什么指导吗?
标签: android json android-volley