简体   繁体   English

为什么在POST上收到500服务器错误?

[英]Why am I getting a 500 server error on POST?

i can't get the Key (token) by post method when i run the app it shows me an error (500) and the response is null,i tried many times but couldn't find the normal solution. 当我运行应用程序时,我无法通过邮寄方法获取钥匙(令牌),它向我显示错误(500),并且响应为空,我尝试了很多次,但找不到正常的解决方案。 To be clear i am putting the Hometask and code below: 为了清楚起见,我将Hometask和代码放在下面:

So the Hometask is : 所以Hometask是:

Create one page Authorization, where there are two fields - Partner Login and Password 创建一页授权,其中有两个字段-合作伙伴登录名和密码

Partner account for your testing: 测试的合作伙伴帐户:

Login: login 登录:登录

Password: password 密码:密码

1) Authorization : 1) 授权

http://client-api.instaforex.com/Home/GetAPIUsageInfo http://client-api.instaforex.com/Home/GetAPIUsageInfo

You need to get token "RequestMoblieCabinetApiToken". 您需要获取令牌“ RequestMoblieCabinetApiToken”。

Request URL: http://client-api.instaforex.com/api/Authentication/RequestMoblieCabinetApiToken 请求网址: http : //client-api.instaforex.com/api/Authentication/RequestMoblieCabinetApiToken

Method: POST 方法:开机自检

Request: 请求:

{ {

"Login": "PARTNER_LOGIN", “登录”:“ PARTNER_LOGIN”,

"Password": "PARTNER_PASSWORD" “密码”:“ PARTNER_PASSWORD”

} }

In response you get "passkey" (your token). 作为响应,您将获得“密码”(您的令牌)。

My code: 我的代码:

ApiInterface Api接口

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 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 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();

                }
            });


    }
}

Please help to get the "passkey" 请帮助获得“密码”

500 status code from server means that your server isn't available at the moment now this issues is not at your end so better communicate with your backend team to resolve this. 来自服务器的500状态代码表示您的服务器目前不可用,因此此问题不会一直存在,因此请与后端团队更好地沟通以解决此问题。 Check this link to understand about status codes better in server response 检查此链接以更好地了解服务器响应中的状态码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM