当我运行应用程序时,我无法通过post方法获取密钥(令牌),它向我显示错误(500)并且响应为空,我尝试了很多次,但是找不到正常的解决方案。 为了清楚起见,我将Hometask和代码放在下面:
因此Hometask是:
创建一页授权,其中有两个字段-合作伙伴登录名和密码
用于测试的合作伙伴帐户:
登录:登录
密码:密码
1)授权:
http://client-api.instaforex.com/Home/GetAPIUsageInfo
您需要获取令牌“ RequestMoblieCabinetApiToken”。
请求网址:http://client-api.instaforex.com/api/Authentication/RequestMoblieCabinetApiToken
方法:开机自检
请求:
{
“登录”:“ PARTNER_LOGIN”,
“密码”:“ PARTNER_PASSWORD”
}
作为回应,您会获得“密码”(您的令牌)。
我的代码:
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类
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 android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.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();
}
});
}
}
请帮助获取“密码”
答案 0 :(得分:-1)
500状态代码表示您的服务器目前不可用,因此此问题不在您的尽头,因此请与您的后端团队更好地沟通以解决此问题。选中此link可以更好地了解服务器响应中的状态码