中使用改造登录会给出错误

中使用改造登录会给出错误

本文介绍了在服务器上使用 json 数据的 android 中使用改造登录会给出错误“除了 Begin_object 但在第 1 行第 1 列中找到字符串"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 API,其中的参数是电子邮件和密码.密码应该是加密的.因为我是第一次使用改造.通过在线传递我的 json,我现在已经生成了模型类.但仍然面临错误除了 Begin_object 但在第 1 行第 1 列中找到字符串".我已经花了很多时间在这上面请帮帮我.

I am having an API in which the params are email and password. The password should be encrypted. As I am using retrofit for the first time.By passing my json online i have generated the model class now.But still facing an error "Except Begin_object but found string in line 1 column 1".I have already spent many hours on this Please help me.

我的 json 是:

{
  "result": {
    "userId": 2,
    "userName": "Ram",
    "emailId": "[email protected]",
    "phoneNumber": "1234567890",
    "eula": 0,
    "status": 0,
    "role": 1,
    "password": "4ba007ae1e4045c3c784fdgefg",
    "creationDate": {
      "date": "2016-12-21 05:45:17.000000",
      "timezone_type": 3,
      "timezone": "India"
    },
    "apiKey": "dsadaskjfhckjhdsfhlksdfsdf4542",
    "lastModified": {
      "date": "2017-02-2 03:25:04.000000",
      "timezone_type": 3,
      "timezone": "India"
    },
    "language": "English",
    "familyId": "",
    "productId": "",
    "partId": "",
    "company": "ABC",
    "address": "xyz",
    "groupIds": "6",
    "notes": null,
    "logo": null,
    "createdBy": 1056,
    "passwordHint": "v$",
    "isArchived": 0,
    "UnSuccessLoginCount": 0,
    "isLocked": 0,
    "lockedTime": {
      "date": "2012:09:02 00:00:00.000000",
      "timezone_type": 3,
      "timezone": "India"
    },
    "lockable": 0
  },
  "message": true
}

MainActivity.java

MainActivity.java

 login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email_enter = email.getText().toString();
                String pass_enter = password.getText().toString();
                String md5pass = md5(pass_enter);
                LoginRequest loginRequest = new LoginRequest();
                loginRequest.setEmail(email_enter);
                loginRequest.setPassword(md5pass);
                singinRequest(loginRequest);

            }
        });
 private void singinRequest(LoginRequest loginRequest) {
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
        ApiInterface apiService = retrofit.create(ApiInterface.class);
        Call<LoginResponse.result> call = apiService.getLogin(loginRequest);
        // Call<LoginResponse> call = apiService.loginWithCredentials(new LoginRequest(email_enter, md5pass));
        call.enqueue(new Callback<LoginResponse.result>() {
            @Override
            public void onResponse(Call<LoginResponse.result> call, Response<LoginResponse.result> response) {
                Log.i("REGISTRATION --->", "Registered" + response.body());
                Intent i = new Intent(MainActivity.this,SecondActivity.class);
                startActivity(i);
            }

            @Override
            public void onFailure(Call<LoginResponse.result> call, Throwable t) {
                Log.i("REGISTRATION --->", "Throwable" + t.toString());
            }
        });
    }

登录响应.java

@SerializedName("result")
@Expose
private Result result;
@SerializedName("message")
@Expose
private Boolean message;

public Result getResult() {
    return result;
}

public void setResult(Result result) {
    this.result = result;
}

public Boolean getMessage() {
    return message;
}

public void setMessage(Boolean message) {
    this.message = message;
}

ApiInterface.java

ApiInterface.java

 @POST("/rest/DoLogin")
    Call<LoginResponse.result> getLogin(@Body LoginRequest loginRequest);

推荐答案

您必须正确解析您的 response 请求.

You have to parse your response request correctly .

if(response.issucess()){
 LoginResponse result = response.body();
 if(result.yourMethodToGetData() != null){
   Intent i = new Intent(MainActivity.this,SecondActivity.class);
   i.putExtra(KEY_BOOK_ID,book.getApiKey());
   startActivity(i);
 }else{
  Loge.e("Error","Error");
 }
 else{
  Log.e("Error", "Api result");
 }
}

我假设您有一个登录按钮.

I assume you have an login button.

现在,在按钮点击监听器中:

Now, inside button click listener:

if (!userEmail.getText().toString().matches("") && !userPassword.getText().toString().matches("")) {
.......
  LoginRequest loginData = new LoginRequest();
  loginData.setEmail(userEmail.getText().toString());
  loginData.setPassword(your_encrypted_password...userPassword.getText().toString());
  signInRequest(loginData);
.......
}

现在,现在在 signInRequest() 方法中:

Now, now in signInRequest() method:

private void signInRequest(final LoginRequest userInfo) {
     ApiClient.getClient().create(ApiInterface.class);
     Call<LoginResponse> call = apiService.loginWithCredentials(userInfo);
        call.enqueue.....
...............
}

这篇关于在服务器上使用 json 数据的 android 中使用改造登录会给出错误“除了 Begin_object 但在第 1 行第 1 列中找到字符串"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 05:27