2017-02-22 41 views
0

У меня есть API, в котором параметры являются электронной почтой и паролем. Пароль должен быть зашифрован. Поскольку я использую retrofit в первый раз. Пройдя мой json онлайн, я создал класс модели now.But все еще сталкивается с ошибкой «Except Begin_object, но найден строка в строке 1 столбец 1». Я уже много часов пробовал на этом Пожалуйста Помоги мне.Войти с помощью модификации в android с данными json на сервере дает ошибку «Except Begin_object, но найден строка в строке 1 столбец 1»

Мой 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

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

LoginResponse.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

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

Какая ошибка? Можете ли вы опубликовать журнал? –

+0

Эта ошибка «Методы обслуживания не могут вернуть пустоту». возникают, если я использую public void в ApiInterface.java и «no annotation found» error, если я использую приведенный выше код –

+0

Посмотрите [здесь] (http://stackoverflow.com/questions/36703737/simple-login-form -with-retrofit), [здесь] (http://stackoverflow.com/questions/33514454/need-help-for-retrofit-login-example) и [здесь] (https://inducesmile.com/android/android -user-login-and-registration-with-retrofit-php-mysql-and-slim-framework /), пропуская серверную часть –

ответ

0
public class LoginResponse { 

    boolean message; 
    result result; 

    public LoginResponse(boolean message, LoginResponse.result result) { 
     this.message = message; 
     this.result = result; 
    } 

    public boolean isMessage() { 
     return message; 
    } 

    public LoginResponse.result getResult() { 
     return result; 
    } 

    public class result { 


     int userId; 
     String userName; 
     String emailId; 
     String phoneNumber; 
     int eula; 
     int status; 
     int role; 
     String password; 
     String language; 
     String familyId; 
     String productId; 
     String partId; 
     String company; 
     String address; 
     String groupIds; 
     String notes; 
     String logo; 
     String createdBy; 
     String passwordHint; 
     int isArchived; 
     int UnSuccessLoginCount; 
     int isLocked; 
     String lockedTime; 
     int lockable; 

     public result(int userId, String userName, String emailId, String phoneNumber, int eula, int status, int role, String password, String language, String familyId, String productId, String partId, String company, String address, String groupIds, String notes, String logo, String createdBy, String passwordHint, int isArchived, int unSuccessLoginCount, int isLocked, String lockedTime, int lockable) { 
      this.userId = userId; 
      this.userName = userName; 
      this.emailId = emailId; 
      this.phoneNumber = phoneNumber; 
      this.eula = eula; 
      this.status = status; 
      this.role = role; 
      this.password = password; 
      this.language = language; 
      this.familyId = familyId; 
      this.productId = productId; 
      this.partId = partId; 
      this.company = company; 
      this.address = address; 
      this.groupIds = groupIds; 
      this.notes = notes; 
      this.logo = logo; 
      this.createdBy = createdBy; 
      this.passwordHint = passwordHint; 
      this.isArchived = isArchived; 
      UnSuccessLoginCount = unSuccessLoginCount; 
      this.isLocked = isLocked; 
      this.lockedTime = lockedTime; 
      this.lockable = lockable; 
     } 

     public int getUserId() { 
      return userId; 
     } 

     public String getUserName() { 
      return userName; 
     } 

     public String getEmailId() { 
      return emailId; 
     } 

     public String getPhoneNumber() { 
      return phoneNumber; 
     } 

     public int getEula() { 
      return eula; 
     } 

     public int getStatus() { 
      return status; 
     } 

     public int getRole() { 
      return role; 
     } 

     public String getPassword() { 
      return password; 
     } 

     public String getLanguage() { 
      return language; 
     } 

     public String getFamilyId() { 
      return familyId; 
     } 

     public String getProductId() { 
      return productId; 
     } 

     public String getPartId() { 
      return partId; 
     } 

     public String getCompany() { 
      return company; 
     } 

     public String getAddress() { 
      return address; 
     } 

     public String getGroupIds() { 
      return groupIds; 
     } 

     public String getNotes() { 
      return notes; 
     } 

     public String getLogo() { 
      return logo; 
     } 

     public String getCreatedBy() { 
      return createdBy; 
     } 

     public String getPasswordHint() { 
      return passwordHint; 
     } 

     public int getIsArchived() { 
      return isArchived; 
     } 

     public int getUnSuccessLoginCount() { 
      return UnSuccessLoginCount; 
     } 

     public int getIsLocked() { 
      return isLocked; 
     } 

     public String getLockedTime() { 
      return lockedTime; 
     } 

     public int getLockable() { 
      return lockable; 
     } 

     public class creationDate { 

     String date; 
     int timezone_type; 
     String timezone; 

      public creationDate(String date, int timezone_type, String timezone) { 
       this.date = date; 
       this.timezone_type = timezone_type; 
       this.timezone = timezone; 
      } 

      public String getDate() { 
       return date; 
      } 

      public int getTimezone_type() { 
       return timezone_type; 
      } 

      public String getTimezone() { 
       return timezone; 
      } 
     } 

     public class lastModified{ 
      String date, timezone_type, timezone; 

      public lastModified(String date, String timezone_type, String timezone) { 
       this.date = date; 
       this.timezone_type = timezone_type; 
       this.timezone = timezone; 
      } 

      public String getDate() { 
       return date; 
      } 

      public String getTimezone_type() { 
       return timezone_type; 
      } 

      public String getTimezone() { 
       return timezone; 
      } 
     } 

     public class lockedTime{ 
      String date, timezone_type, timezone; 

      public lockedTime(String date, String timezone_type, String timezone) { 
       this.date = date; 
       this.timezone_type = timezone_type; 
       this.timezone = timezone; 
      } 

      public String getDate() { 
       return date; 
      } 

      public String getTimezone_type() { 
       return timezone_type; 
      } 

      public String getTimezone() { 
       return timezone; 
      } 
     } 
    } 
} 
+0

Нет, это опять бросает то же самое исключение –

+0

Я отвечу тебе некоторое время –

+0

Хорошо, Эй Доброе утро. У вас есть решение? –

0

Попробовать это это будет работать наверняка

public class LoginResponse { 

    boolean message; 
    result result; 

    public LoginResponse(boolean message, LoginResponse.result result) { 
     this.message = message; 
     this.result = result; 
    } 

    public boolean isMessage() { 
     return message; 
    } 

    public LoginResponse.result getResult() { 
     return result; 
    } 

    public class result { 


     int userId; 
     String userName; 
     String emailId; 
     String phoneNumber; 
     int eula; 
     int status; 
     int role; 
     String password; 
     String language; 
     String familyId; 
     String productId; 
     String partId; 
     String company; 
     String address; 
     String groupIds; 
     String notes; 
     String logo; 
     String createdBy; 
     String passwordHint; 
     int isArchived; 
     int UnSuccessLoginCount; 
     int isLocked; 
     int lockable; 
     creationDate creationDate; 
     lockedTime lockedTime; 
     lastModified lastModified; 

     public result(int userId, String userName, String emailId, String phoneNumber, int eula, int status, int role, String password, String language, String familyId, String productId, String partId, String company, String address, String groupIds, String notes, String logo, String createdBy, String passwordHint, int isArchived, int unSuccessLoginCount, int isLocked, int lockable, LoginResponse.result.creationDate creationDate, LoginResponse.result.lockedTime lockedTime, LoginResponse.result.lastModified lastModified) { 
      this.userId = userId; 
      this.userName = userName; 
      this.emailId = emailId; 
      this.phoneNumber = phoneNumber; 
      this.eula = eula; 
      this.status = status; 
      this.role = role; 
      this.password = password; 
      this.language = language; 
      this.familyId = familyId; 
      this.productId = productId; 
      this.partId = partId; 
      this.company = company; 
      this.address = address; 
      this.groupIds = groupIds; 
      this.notes = notes; 
      this.logo = logo; 
      this.createdBy = createdBy; 
      this.passwordHint = passwordHint; 
      this.isArchived = isArchived; 
      UnSuccessLoginCount = unSuccessLoginCount; 
      this.isLocked = isLocked; 
      this.lockable = lockable; 
      this.creationDate = creationDate; 
      this.lockedTime = lockedTime; 
      this.lastModified = lastModified; 
     } 

     public LoginResponse.result.creationDate getCreationDate() { 
      return creationDate; 
     } 

     public LoginResponse.result.lastModified getLastModified() { 
      return lastModified; 
     } 

     public int getUserId() { 
      return userId; 
     } 

     public String getUserName() { 
      return userName; 
     } 

     public String getEmailId() { 
      return emailId; 
     } 

     public String getPhoneNumber() { 
      return phoneNumber; 
     } 

     public int getEula() { 
      return eula; 
     } 

     public int getStatus() { 
      return status; 
     } 

     public int getRole() { 
      return role; 
     } 

     public String getPassword() { 
      return password; 
     } 

     public String getLanguage() { 
      return language; 
     } 

     public String getFamilyId() { 
      return familyId; 
     } 

     public String getProductId() { 
      return productId; 
     } 

     public String getPartId() { 
      return partId; 
     } 

     public String getCompany() { 
      return company; 
     } 

     public String getAddress() { 
      return address; 
     } 

     public String getGroupIds() { 
      return groupIds; 
     } 

     public String getNotes() { 
      return notes; 
     } 

     public String getLogo() { 
      return logo; 
     } 

     public String getCreatedBy() { 
      return createdBy; 
     } 

     public String getPasswordHint() { 
      return passwordHint; 
     } 

     public int getIsArchived() { 
      return isArchived; 
     } 

     public int getUnSuccessLoginCount() { 
      return UnSuccessLoginCount; 
     } 

     public int getIsLocked() { 
      return isLocked; 
     } 

     public LoginResponse.result.lockedTime getLockedTime() { 
      return lockedTime; 
     } 

     public int getLockable() { 
      return lockable; 
     } 

     public class creationDate { 

      String date; 
      int timezone_type; 
      String timezone; 

      public creationDate(String date, int timezone_type, String timezone) { 
       this.date = date; 
       this.timezone_type = timezone_type; 
       this.timezone = timezone; 
      } 

      public String getDate() { 
       return date; 
      } 

      public int getTimezone_type() { 
       return timezone_type; 
      } 

      public String getTimezone() { 
       return timezone; 
      } 
     } 

     public class lastModified { 
      String date, timezone_type, timezone; 

      public lastModified(String date, String timezone_type, String timezone) { 
       this.date = date; 
       this.timezone_type = timezone_type; 
       this.timezone = timezone; 
      } 

      public String getDate() { 
       return date; 
      } 

      public String getTimezone_type() { 
       return timezone_type; 
      } 

      public String getTimezone() { 
       return timezone; 
      } 
     } 

     public class lockedTime { 
      String date, timezone_type, timezone; 

      public lockedTime(String date, String timezone_type, String timezone) { 
       this.date = date; 
       this.timezone_type = timezone_type; 
       this.timezone = timezone; 
      } 

      public String getDate() { 
       return date; 
      } 

      public String getTimezone_type() { 
       return timezone_type; 
      } 

      public String getTimezone() { 
       return timezone; 
      } 
     } 
    } 
} 
0

Вы должны правильно разобрать ваш response запрос.

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"); 
} 
} 

Я предполагаю, что у вас есть кнопка входа в систему.

Теперь внутри кнопки мыши слушателю:

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() метод:

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

Thanx для ответа !!! Но мой отладчик не достигает в onResponse, он перескакивает на onFailure и бросает исключение «ожидаемый begin_object, но был строкой в ​​строке 1 столбец 1» –

+0

Где ваши классы LoginRequest? –

+0

Здесь: -публичный класс LoginRequest { public void setPassword (String password) { this.password = password; } public void setEmail (String email) { this.email = email; } @SerializedName ("password") String password; @SerializedName ("email") String email; } –

0

In Your MainActivity.class Управление этой линии: LoginResponse книга = response.body() ;

String email_enter = email.getText().toString(); 
     String pass_enter = password.getText().toString(); 
     String md5pass = md5(pass_enter); 
     ApiInterface apiService = 
       ApiClient.getClient().create(ApiInterface.class); 
     Call<LoginResponse> call = apiService.loginWithCredentials(new LoginRequest(email_enter, md5pass)); 
     call.enqueue(new Callback<LoginResponse>() { 
      @Override 
      public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) { 
       Log.i("REGISTRATION --->", "Registered" + response); 
       LoginResponse book = response.body(); 
         Intent i = new Intent(MainActivity.this,SecondActivity.class); 
         i.putExtra(KEY_BOOK_ID,book.getApiKey()); 
         startActivity(i); 


      } 

      @Override 
      public void onFailure(Call<LoginResponse> call, Throwable t) { 
       Log.i("REGISTRATION --->", "Throwable" + t.toString()); 
       Intent i = new Intent(MainActivity.this,SecondActivity.class); 
       startActivity(i);     } 
     }); 
+0

Это не здесь, по ошибке было написано. удалите это уже –

+0

Я думаю, что зритель команды будет хорош в урском деле. Предоставьте мне свой идентификатор зрителя вашей команды и пройдете –

+0

Я не использовал зрителя команды. –