java为什么我在POST上遇到500服务器错误?
我无法通过post方法获取密钥(令牌),当我运行应用程序时,它显示了一个错误(500),响应为空,我尝试了很多次,但找不到正常的解决方案。 为了清楚起见,我将Hometask和代码放在下面:
所以家庭任务是:
创建一页授权,其中有两个字段-合作伙伴登录和密码
测试的合作伙伴帐户:
登录:登录
密码:密码
1)授权:
http://client-api.instaforex.com/Home/GetAPIUsageInfo
您需要获得令牌“requestmobbinetapitoken”
请求URL:http://client-api.instaforex.com/api/Authentication/RequestMoblieCabinetApiToken
方法:邮寄
请求:
{
“登录”:“合作伙伴登录”
“密码”:“合作伙伴密码”
}
作为回应,您将获得“密钥”(您的令牌)
我的代码:
ApiInterface
package com.example.instaforexapp.Rest;
import com.example.instaforexapp.Modal.ApiAccount;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
public interface InstaForexApi {
@FormUrlEncoded
@POST("api/Authentication/RequestMoblieCabinetApiToken")
Call<ApiAccount> createAccount( @Field("Login") String login,
@Field("Password") String password);
}
ApiClient
package com.example.instaforexapp.Rest;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class ApiClient {
private static final String BASE_URL = "http://client-api.instaforex.com/";
private static Retrofit retrofit = null;
public static Retrofit getRetrofit() {
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.level(HttpLoggingInterceptor.Level.BODY);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor)
.build();
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
}
return retrofit;
}
}
ApiAccount Class
import com.google.gson.annotations.SerializedName;
public class ApiAccount {
@SerializedName("Login")
private String login;
@SerializedName("Password")
private String password;
public ApiAccount(String login, String password) {
this.login = login;
this.password = password;
}
public String getLogin() {
return login;
}
public String getPassword() {
return password;
}
}
MainActivity
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.Toast;
import com.example.instaforexapp.Modal.ApiAccount;
import com.example.instaforexapp.Rest.ApiClient;
import com.example.instaforexapp.Rest.InstaForexApi;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
private EditText txt_login,txt_password;
private Button btn_confirm;
public static final String TAG = "com.MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt_login = findViewById(R.id.txt_login);
txt_password = findViewById(R.id.txt_pass);
btn_confirm = findViewById(R.id.btn_confirm);
btn_confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String login = txt_login.getText().toString();
String password= txt_password.getText().toString();
createAccount(login,password);
Log.i(TAG, "login :"+login +" password: "+password);
}
});
}
private void createAccount(String login,String password){
InstaForexApi api = ApiClient.getRetrofit().create(InstaForexApi.class);
Call<ApiAccount> call = api.createAccount(login,password);
call.enqueue(new Callback<ApiAccount>() {
@Override
public void onResponse( Call<ApiAccount> call, Response<ApiAccount> response) {
if (!response.isSuccessful()){
Toast.makeText(MainActivity.this, "Error: "+response.code(),
Toast.LENGTH_SHORT).show();
}
ApiAccount account = response.body();
String toast = null;
if (account != null) {
toast = account.getLogin()+" : " + account.getPassword();
}
Toast.makeText(MainActivity.this, toast, Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<ApiAccount> call, Throwable t) {
Toast.makeText(MainActivity.this, t.getMessage(),
Toast.LENGTH_SHORT).show();
}
});
}
}
请帮助获取“密钥”
# 1 楼答案
来自服务器的500状态代码意味着您的服务器目前不可用,现在这个问题不在您这边,所以最好与您的后端团队沟通以解决这个问题。选中此link可以更好地了解服务器响应中的状态代码