使用翻新发送POST请求到服务器

时间:2020-04-07 07:11:21

标签: android retrofit

我是Retrofit的新手。我正在尝试使用用户名和密码将登录请求发送到服务器。当我发送 username作为用户; 45 和ASCII编码时,它将转换为 username = user%3B和password = test%40123 。由于此服务器拒绝了我的登录请求。我知道它的ASCII编码。我想发送输入的值。 日志是[https://i.stack.imgur.com/YlgnL.png]

> MainActivity.java

private void userLogin() {

        String j_username = editTextUserName.getText().toString().trim();
        String j_password = editTextPassword.getText().toString().trim();

        if (j_username.isEmpty()) {
            editTextUserName.setError("UserName is required");
            editTextUserName.requestFocus();
            return;
        }

        if (j_password.isEmpty()) {
            editTextPassword.setError("Password is required");
            editTextPassword.requestFocus();
            return;
        }

        System.out.println("Call");
        Call<Post> call = ApiUtils.getInstance()
                .getAPIService()
                .savePost(j_username, j_password);

        System.out.println("call.enqueue");

        call.enqueue(new Callback<Post>() {
            @Override
            public void onResponse(Call<Post> call, Response<Post> response) {
                try {
                    String s = response.body().toString();
                    Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();
                    Toast.makeText(getApplicationContext(), "onResponse Successful !!", Toast.LENGTH_LONG).show();
                }
                catch(Exception e){
                    Toast.makeText(getApplicationContext(), "Exception Occurred !!", Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }

                System.out.println("OnResponse");
                String s = response.body().toString();
                Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFailure(Call<Post> call, Throwable t) {
                System.out.println(t.getMessage());
                System.out.println("OnFailure");
                Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();
            }
        })

API.java

gson = new GsonBuilder()
                .setLenient()
                .create();

        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();

        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .build();

        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

        System.out.println(retrofit);

0 个答案:

没有答案