问题
I have a web api and an application. So I want to a Register app but I have a problem. I use the azure. There is my registerapi (interface)
@FormUrlEncoded
@POST("/application/json")
public void insertUser(
@Field("Username") String Username,
@Field("Password") String Password,
@Field("Email") String Email,
Callback<Response> callback);
and my mainactivty.java page
public class MainActivity extends AppCompatActivity {
private EditText editTextUsername;
private EditText editTextPassword;
private EditText editTextEmail;
private Button buttonRegister;
final public static String ROOT_URL = "http://bsapmusic.azurewebsites.net/api/music/register";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextUsername = (EditText) findViewById(R.id.etusername);
editTextPassword = (EditText) findViewById(R.id.etpassword);
editTextEmail = (EditText) findViewById(R.id.etmail);
buttonRegister = (Button) findViewById(R.id.btnkayit);
buttonRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
insertUser();
}
});
}
private void insertUser()
{
RestAdapter adapter =new RestAdapter.Builder().setEndpoint(ROOT_URL).build();
RegisterAPI api = adapter.create(RegisterAPI.class);
api.insertUser(
editTextUsername.getText().toString(),
editTextPassword.getText().toString(),
editTextEmail.getText().toString(),
new Callback<Response>() {
@Override
public void success(Response result, Response response) {
BufferedReader reader = null;
String output = "";
try{
reader=new BufferedReader(new InputStreamReader(result.getBody().in()));
output=reader.readLine();
}
catch (IOException e){
e.printStackTrace();
}
Toast.makeText(MainActivity.this,output,Toast.LENGTH_LONG).show();
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(MainActivity.this, error.toString(),Toast.LENGTH_LONG).show();
}
});
};
and I get retrofit 404 not found.
回答1:
Assuming http://bsapmusic.azurewebsites.net/api/music/register
is the url to your register method in your API:
A 404 means there is no API for this url.
Your baseurl is http://bsapmusic.azurewebsites.net/api/music/register
After this baseurl the path inside the @post is appended. This will result in the url: http://bsapmusic.azurewebsites.net/api/music/register/application/json
.
The url to your register method is probably the first url.
The baseurl should the url to the root url of your api, in your case this probably is: http://bsapmusic.azurewebsites.net/api/
. In the @GET, @POST, @PUT you set the path to a specific api method. In your case that would be @POST("music/register")
. You don't set the type of data that is sent to the API in here.
来源:https://stackoverflow.com/questions/39059026/retrofit-404-not-found-web-api