【问题标题】:not getting data from php Api using post method没有使用 post 方法从 php Api 获取数据
【发布时间】:2019-03-07 18:46:40
【问题描述】:

我正在尝试从 API 获取数据,作为初学者,我必须使用 post 方法用户名和密码我收到错误 400,因为我无法将数据传递到服务器 这是代码

API接口:

public interface APIInterface {
@POST("http://tvsfit.mytvs.in/reporting/vrm/api/test_new/int/gettabledata.php")
Call<LoginResponse> createUser(@Body LoginResponse login);
}

API客户端:

class APIClient {

public static final String BASE_URL = "http://tvsfit.mytvs.in/reporting/vrm/api/test_new/int/gettabledata.php/";
private static Retrofit retrofit = null;

public static Retrofit getClient() {
    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    return retrofit;
}
}

getter-setter:

public class LoginResponse {


@SerializedName("username")
public String UserId;
@SerializedName("FirstName")
public String FirstName;
@SerializedName("LastName")
public String LastName;
@SerializedName("ProfilePicture")
public String ProfilePicture;
@SerializedName("Password")
public String Password;
   public LoginResponse(String UserId, String Password) {
    this.UserId = UserId;
    this.Password = Password;
}

我收到这样的错误

`Response{protocol=http/1.1, code=400, message=Bad Request, url=http://tvsfit.mytvs.in/reporting/vrm/api/test_new/int/gettabledata.php}'

我做错的任何方向都会有所帮助 { “用户名”:“测试”, “密码”:“123456” } 我必须得到的样本输出

{"TABLE_DATA":"{\"data\":[[\"Tiger Nixon\",\"System Architect\",\"Edinburgh\",\"5421\",\"2011\/04\/25\",\"$320,800\"]...

活动方式:

 private void loginRetrofit2Api(String userId, String password) {
    final LoginResponse login = new LoginResponse(userId, password);
    Call<LoginResponse> call1 = apiInterface.createUser(login);
    call1.enqueue(new Callback<LoginResponse>() {
        @Override
        public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
            Log.e("Login", response.toString());


        }

        @Override
        public void onFailure(Call<LoginResponse> call, Throwable t) {
            Toast.makeText(getApplicationContext(), "onFailure called ", Toast.LENGTH_SHORT).show();
            call.cancel();
        }
    });
}

public boolean checkValidation() {
    userId = et_Email.getText().toString();
    password = et_Pass.getText().toString();
    if (et_Email.getText().toString().trim().equals("")) {
        CommonMethod.showAlert("UserId Cannot be left blank", MainActivity.this);
        return false;
    } else if (et_Pass.getText().toString().trim().equals("")) {
        CommonMethod.showAlert("password Cannot be left blank", MainActivity.this);
        return false;
    }
    return true;
}

CommonMethod 类:

public  static boolean isNetworkAvailable(Context ctx) {
    ConnectivityManager connectivityManager
            = (ConnectivityManager)ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

public static void showAlert(String message, Activity context) {

    final AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setMessage(message).setCancelable(false)
            .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                }
            });
    try {
        builder.show();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

数据:

【问题讨论】:

  • 您是否尝试过对帖子 url 进行 url 编码?在发送请求之前尝试URLEncoder.encode(BASE_URL, "UTF-8");
  • 不,先生,但它发生了错误,因为用户名和密码没有发送到 api 我不认为那部分有任何错误。
  • 您可以分享您提出请求的代码吗?
  • @S.Souza 我已经添加了几乎所有的代码
  • 这个api有文档吗?如果有,可以分享一下吗?

标签: android api retrofit2


【解决方案1】:

你在这里做的很少。

首先,您需要正确设置您的基本网址。

public static final String BASE_URL = "http://tvsfit.mytvs.in/"

现在相应地更改方法调用中的路径。您需要设置将在 Base URL 之后添加的路径。

public interface APIInterface {
@POST("reporting/vrm/api/test_new/int/gettabledata.php")
Call<LoginResponse> createUser(@Body LoginResponse login);
}

序列化名称区分大小写,您需要将它们与参数名称完全匹配。

@SerializedName("password")    //in sample request you are using password and not Password
public String Password;

【讨论】:

  • 兄弟我无法获取数据Response{protocol=http/1.1, code=200, message=OK是日志如何获取数据作为以下数据我将如何处理响应?
  • 兄弟我如何将数据放入数组列表中,因为没有数据对象我可以转换字符串但我什至得到字符串值
【解决方案2】:

你应该做的是:

public interface APIInterface {
@POST("http://tvsfit.mytvs.in/reporting/vrm/api/test_new/int/gettabledata.php")
Call<LoginResponse> createUser(@String userId, @String password);
}

LoginResponse 是您处理响应的类。因此,在尝试登录时传递作为参数没有多大意义。 你的样子:

Call<LoginResponse> call1 = apiInterface.createUser(userId, password);

我当然可能是错的,所以请检查文档以准确了解所有参数。

【讨论】:

  • 先生,但我在 Loginresponse 文件中使用了一个构造函数,这就是我这样做的原因
【解决方案3】:

你有没有在 Postman 测试过这个 api 是否运行良好 当我在邮递员测试这个 api 时。它显示错误 400 并显示错误消息 {"ErrorDescription":"Mandatory parameters missing","Error":400} .it' 你没有传递所有参数那个api调用。看到这个截图。 api error screenshot

【讨论】:

  • api 正在工作,图片显示了 api 的输出
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-31
  • 2012-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-16
  • 1970-01-01
相关资源
最近更新 更多