我试图发送带有某些参数的JsonObject的发布请求以在db上创建用户,但是我总是收到HTTP 400-错误的请求。我试图覆盖getHeaders(),getBodyContent(),就像其他类似主题的答案所说的那样,但是我的代码仍然无法正常工作。我什至试图从头开始逐步遵循教程,只是看它是否可行,但是结果是一样的。
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta charset="utf-8">
<link rel="stylesheet" href="/stylesheets/style.css">
</head>
<body>
<h1></h1>
<h4>Sort by:</h4>
<div class="row">
<div class="column"><select id="level" style="margin-bottom:20px;" onchange="updateHeatMap()"><option value="class" selected>Class</option><option value="family">Family</option><option value="genus">Genus</option><option value="order">Order</option><option value="phylum">Phylum</option><option value="species">Species</option><option value="subclass">Subclass</option><option value="suborder">Suborder</option><option value="subphylum">Subphylum</option><option value="superphylum">Superphylum</option></select></div>
<div
class="column">
<div id="thelevel"></div>
</div>
</div>
<div id="heatmap"></div>
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="javascripts/heatmap9.js"></script>
</body>
</html>
这是我的Web服务代码:
package com.example.sandrini.pokeagenda;
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.ProgressBar;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class CreateTrainerActivity extends AppCompatActivity {
private EditText registerUsername;
private EditText registerEmail;
private EditText registerPassword;
private Button registerTrainerButton;
private ProgressBar registerTrainerProgressbar;
private static final String URL = "http://192.168.25.34:8081/PokedexWS/webresources/pokews/poke/insert/trainer";
private static final String KEY_USERNAME = "username";
private static final String KEY_EMAIL = "email";
private static final String KEY_PASSWORD = "password";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_trainer);
registerUsername = (EditText) findViewById(R.id.register_username);
registerEmail = (EditText) findViewById(R.id.register_email);
registerPassword = (EditText) findViewById(R.id.register_password);
registerTrainerButton = (Button) findViewById(R.id.register_trainer_button);
registerTrainerProgressbar = (ProgressBar) findViewById(R.id.register_trainer_progressbar);
registerTrainerProgressbar.setVisibility(View.INVISIBLE);
registerTrainerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
registerTrainer();
registerTrainerProgressbar.setVisibility(View.VISIBLE);
}
});
}
private void registerTrainer(){
final String username = registerUsername.getText().toString();
final String email = registerEmail.getText().toString();
final String password = registerPassword.getText().toString();
/*Map<String, String> params = new HashMap<>();
params.put(KEY_USERNAME, username);
params.put(KEY_EMAIL, email);
params.put(KEY_PASSWORD, password);*/
JSONObject jsonObject = new JSONObject();
try{
jsonObject.put(KEY_USERNAME, username);
jsonObject.put(KEY_EMAIL, email);
jsonObject.put(KEY_PASSWORD, password);
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, URL, jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Toast.makeText(getApplicationContext(), "Registered successfully", Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json; charset=utf-8");
headers.put("User-agent", "My useragent");
return headers;
}
@Override
public String getBodyContentType() {
return "application/json";
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonRequest);
}
}