我是 Android 编程和 JSON 的新手。我正在尝试使用 volley post 请求调用身份验证 API。我正在尝试获取用户插入的用户名和密码并将其发送到 API 并返回响应。看来我无法直接将输入传递到请求的 JSON 正文中。您能否指出一种将从用户输入获取的数据输入到 JSON 正文请求的方法。我附上了我的代码,这样您就可以得到更好的想法。谢谢。
公共类 LoginActivity 扩展 AppCompatActivity {
Button login_btn;
EditText input_username, input_password;
String url;
RequestQueue queue;
StringRequest stringRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login_btn = findViewById(R.id.btn_login);
input_username=findViewById(R.id.editText_username);
input_password=findViewById(R.id.editText_password);
url="https://restful-booker.herokuapp.com/auth";
try {
queue=Volley.newRequestQueue(this);
JSONObject jsonBody = new JSONObject();
String username = input_username.getText().toString();
String password = input_password.getText().toString();
jsonBody.put("username", username);
jsonBody.put("password", password);
final String mRequestBody = jsonBody.toString();
stringRequest=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(LoginActivity.this, response,Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(LoginActivity.this, error.toString(),Toast.LENGTH_LONG).show();
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap header= new HashMap();
header.put ("accept","*/*");
header.put ("Content-Type","application/json");
return header;
}
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
return null;
}
}
//Return Response code
};
login_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
queue.add(stringRequest);
}
});
}
catch (JSONException e) {
e.printStackTrace();
}
}
}
我认为您直接在 onCreate 而不是 onClick 获取 edittext 的文本
这会起作用
public class LoginActivity extends AppCompatActivity {
Button login_btn;
EditText input_username, input_password;
String url;
RequestQueue queue;
StringRequest stringRequest;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
login_btn = findViewById(R.id.btn_login);
input_username=findViewById(R.id.editText_username);
input_password=findViewById(R.id.editText_password);
url="https://restful-booker.herokuapp.com/auth";
queue=Volley.newRequestQueue(this);
login_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username = input_username.getText().toString();
String password = input_password.getText().toString();
try {
JSONObject jsonBody = new JSONObject();
jsonBody.put("username", username);
jsonBody.put("password", password);
final String mRequestBody = jsonBody.toString();
stringRequest=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(LoginActivity.this, response,Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(LoginActivity.this, error.toString(),Toast.LENGTH_LONG).show();
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap header= new HashMap();
header.put ("accept","*/*");
header.put ("Content-Type","application/json");
return header;
}
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
return null;
}
}
};
queue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
希望它有效!!