4

I'm trying to create forget password script for my app .When i run my apk and try to rest my password i got error which says Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $ Error, Everything is working fine in my apk i can login and logout or register but when i reset password i got JsonReader.setLenient(true) error. My apk json is working and it's correct. Please Help me. I'm Sharing my code.

{
   "operation":"resPassReq",
   "user":{
       "email":"name@example.com"

   }
}

and if the request is sucess the response would be similar to,

{
  "result": "success",
  "message": "Check your mail for reset password code."
}

For finishing password reset process the request would be similar to,

{
   "operation":"resPass",
   "user":{
       "email":"name@example.com",
       "code":"bcfqa3",
       "password":"rjamalw"

   }
}

and if the request is success the response would be similar to,

{
  "result": "success",
  "message": "Password Changed Successfully"
}

ResetPasswordFragment.java

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ResetPasswordFragment extends Fragment implements View.OnClickListener{

    private AppCompatButton btn_reset;
    private EditText et_email,et_code,et_password;
    private TextView tv_timer;
    private ProgressBar progress;
    private boolean isResetInitiated = false;
    private String email;
    private CountDownTimer countDownTimer;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_password_reset,container,false);
        initViews(view);
        return view;
    }

    private void initViews(View view){

        btn_reset = (AppCompatButton)view.findViewById(R.id.btn_reset);
        tv_timer = (TextView)view.findViewById(R.id.timer);
        et_code = (EditText)view.findViewById(R.id.et_code);
        et_email = (EditText)view.findViewById(R.id.et_email);
        et_password = (EditText)view.findViewById(R.id.et_password);
        et_password.setVisibility(View.GONE);
        et_code.setVisibility(View.GONE);
        tv_timer.setVisibility(View.GONE);
        btn_reset.setOnClickListener(this);
        progress = (ProgressBar)view.findViewById(R.id.progress);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.btn_reset:

                if(!isResetInitiated) {


                    email = et_email.getText().toString();
                    if (!email.isEmpty()) {
                        progress.setVisibility(View.VISIBLE);
                        initiateResetPasswordProcess(email);
                    } else {

                        Snackbar.make(getView(), "Fields are empty !", Snackbar.LENGTH_LONG).show();
                    }
                } else {

                    String code = et_code.getText().toString();
                    String password = et_password.getText().toString();

                    if(!code.isEmpty() && !password.isEmpty()){

                        finishResetPasswordProcess(email,code,password);
                    } else {

                        Snackbar.make(getView(), "Fields are empty !", Snackbar.LENGTH_LONG).show();
                    }

                }

                break;
        }
    }

    private void initiateResetPasswordProcess(String email){

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RequestInterface requestInterface = retrofit.create(RequestInterface.class);

        User user = new User();
        user.setEmail(email);
        ServerRequest request = new ServerRequest();
        request.setOperation(Constants.RESET_PASSWORD_INITIATE);
        request.setUser(user);
        Call<ServerResponse> response = requestInterface.operation(request);

        response.enqueue(new Callback<ServerResponse>() {
            @Override
            public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {

                ServerResponse resp = response.body();
                Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();

                if(resp.getResult().equals(Constants.SUCCESS)){

                    Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();
                    et_email.setVisibility(View.GONE);
                    et_code.setVisibility(View.VISIBLE);
                    et_password.setVisibility(View.VISIBLE);
                    tv_timer.setVisibility(View.VISIBLE);
                    btn_reset.setText("Change Password");
                    isResetInitiated = true;
                    startCountdownTimer();

                } else {

                    Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();

                }
                progress.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onFailure(Call<ServerResponse> call, Throwable t) {

                progress.setVisibility(View.INVISIBLE);
                Log.d(Constants.TAG,"failed");
                Snackbar.make(getView(), t.getLocalizedMessage(), Snackbar.LENGTH_LONG).show();
                Log.d(Constants.TAG, t.getLocalizedMessage());
            }
        });
    }

    private void finishResetPasswordProcess(String email,String code, String password){

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RequestInterface requestInterface = retrofit.create(RequestInterface.class);

        User user = new User();
        user.setEmail(email);
        user.setCode(code);
        user.setPassword(password);
        ServerRequest request = new ServerRequest();
        request.setOperation(Constants.RESET_PASSWORD_FINISH);
        request.setUser(user);
        Call<ServerResponse> response = requestInterface.operation(request);

        response.enqueue(new Callback<ServerResponse>() {
            @Override
            public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {

                ServerResponse resp = response.body();
                Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();

                if(resp.getResult().equals(Constants.SUCCESS)){

                    Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();
                    countDownTimer.cancel();
                    isResetInitiated = false;
                    goToLogin();

                } else {

                    Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();

                }
                progress.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onFailure(Call<ServerResponse> call, Throwable t) {

                progress.setVisibility(View.INVISIBLE);
                Log.d(Constants.TAG,"failed");
                Snackbar.make(getView(), t.getLocalizedMessage(), Snackbar.LENGTH_LONG).show();

            }
        });
    }

    private void startCountdownTimer(){
        countDownTimer = new CountDownTimer(120000, 1000) {

            public void onTick(long millisUntilFinished) {
                tv_timer.setText("Time remaining : " + millisUntilFinished / 1000);
            }

            public void onFinish() {
                Snackbar.make(getView(), "Time Out ! Request again to reset password.", Snackbar.LENGTH_LONG).show();
                goToLogin();
            }
        }.start();
    }

    private void goToLogin(){

        Fragment login = new LoginFragment();
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.replace(R.id.fragment_frame,login);
        ft.commit();
    }
}

RequestInterface.java

public interface RequestInterface {

    @POST("req/")
    Call<ServerResponse> operation(@Body ServerRequest request);

}

But When i change my code like below, Everything is working fine, but problem is that got response onFailure

private void initiateResetPasswordProcess(String email){
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RequestInterface requestInterface = retrofit.create(RequestInterface.class);

        User user = new User();
        user.setEmail(email);
        ServerRequest request = new ServerRequest();
        request.setOperation(Constants.RESET_PASSWORD_INITIATE);
        request.setUser(user);
        Call<ServerResponse> response = requestInterface.operation(request);

        response.enqueue(new Callback<ServerResponse>() {
            @Override
            public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {

                ServerResponse resp = response.body();
                Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();

                if(resp.getResult().equals(Constants.SUCCESS)){

                    Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();
                    et_email.setVisibility(View.GONE);
                    et_code.setVisibility(View.VISIBLE);
                    et_password.setVisibility(View.VISIBLE);
                    tv_timer.setVisibility(View.VISIBLE);
                    btn_reset.setText("Change Password");
                    isResetInitiated = true;
                    startCountdownTimer();

                } else {

                    Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();

                }
                progress.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onFailure(Call<ServerResponse> call, Throwable t) {

                progress.setVisibility(View.INVISIBLE);
                Log.d(Constants.TAG,"failed");
               // Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();
                et_email.setVisibility(View.GONE);
                et_code.setVisibility(View.VISIBLE);
                et_password.setVisibility(View.VISIBLE);
                tv_timer.setVisibility(View.VISIBLE);
                btn_reset.setText("Change Password");
                isResetInitiated = true;
                startCountdownTimer();
               // Snackbar.make(getView(), t.getLocalizedMessage(), Snackbar.LENGTH_LONG).show();


            }
        });
    }

    private void finishResetPasswordProcess(String email,String code, String password){
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

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

        RequestInterface requestInterface = retrofit.create(RequestInterface.class);

        User user = new User();
        user.setEmail(email);
        user.setCode(code);
        user.setPassword(password);
        ServerRequest request = new ServerRequest();
        request.setOperation(Constants.RESET_PASSWORD_FINISH);
        request.setUser(user);
        Call<ServerResponse> response = requestInterface.operation(request);

        response.enqueue(new Callback<ServerResponse>() {
            @Override
            public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {

                ServerResponse resp = response.body();
                Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();

                if(resp.getResult().equals(Constants.SUCCESS)){

                    Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();
                    countDownTimer.cancel();
                    isResetInitiated = false;
                    goToLogin();

                } else {

                    Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();

                }
                progress.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onFailure(Call<ServerResponse> call, Throwable t) {

                progress.setVisibility(View.INVISIBLE);
                Log.d(Constants.TAG,"failed");
               // Snackbar.make(getView(), t.getLocalizedMessage(), Snackbar.LENGTH_LONG).show();
                countDownTimer.cancel();
                isResetInitiated = false;
                goToLogin();

            }
        });
    }

ServerResponse.java

public class ServerResponse {

    private String result;
    private String message;
    private User user;

    public String getResult() {
        return result;
    }

    public String getMessage() {
        return message;
    }

    public User getUser() {
        return user;
    }
}

ServerRequest.java

public class ServerRequest {

    private String operation;
    private User user;

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
Misty
  • 41
  • 1
  • 1
  • 5
  • 1
    Welcome to Stack Overflow! To give you a great answer, it might help us if you have a glance at [ask] if you haven't already. It might be also useful if you could provide a [mcve]. – Mat Jan 19 '17 at 09:34
  • You should share the error you get. You can print it by replacing ```Log.d(Constants.TAG,"failed");``` with ```Log.d(Constants.TAG,"failed",t);``` in the onFailure callbacks. You may also want to output whatever relevant information the ```call``` object contains. – Haem Jan 19 '17 at 09:45
  • i got this error - JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $ Error – Misty Jan 19 '17 at 09:49
  • Well, obviously the API is not returning json when you reset your password – OneCricketeer Jan 19 '17 at 12:59
  • Please tell, how can I solve it.. – Misty Jan 19 '17 at 20:48
  • you can try this [link] https://stackoverflow.com/questions/39918814/use-jsonreader-setlenienttrue-to-accept-malformed-json-at-line-1-column-1-path – rizujikeda Jan 19 '19 at 14:13
  • i am getting this error at compile time how can i solve it no any response change then also get this error at compile time – Mehul Tank Feb 12 '19 at 13:14

3 Answers3

1

I have faced this problem and finally, I solved it.

You have to make all $row in your API as the same name in the table on the database, like this

    "book_chalet_id" => $row['book_chalet_id'],

so in the table must has column book_chalet_id.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Momen Zaqout
  • 1,508
  • 1
  • 16
  • 17
0
    private void initiateResetPasswordProcess(String email){
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
            Gson gson = new GsonBuilder()
                           .setLenient()
                           .create();
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(Constants.BASE_URL)
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build();

            RequestInterface requestInterface = retrofit.create(RequestInterface.class);

            User user = new User();
            user.setEmail(email);
            ServerRequest request = new ServerRequest();
            request.setOperation(Constants.RESET_PASSWORD_INITIATE);
            request.setUser(user);
            Call<ServerResponse> response = requestInterface.operation(request);

            response.enqueue(new Callback<ServerResponse>() {
                @Override
                public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {

                    ServerResponse resp = response.body();
                    Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();

                    if(resp.getResult().equals(Constants.SUCCESS)){

                        Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();
                        et_email.setVisibility(View.GONE);
                        et_code.setVisibility(View.VISIBLE);
                        et_password.setVisibility(View.VISIBLE);
                        tv_timer.setVisibility(View.VISIBLE);
                        btn_reset.setText("Change Password");
                        isResetInitiated = true;
                        startCountdownTimer();

                    } else {

                        Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();

                    }
                    progress.setVisibility(View.INVISIBLE);
                }

                @Override
                public void onFailure(Call<ServerResponse> call, Throwable t) {

                    progress.setVisibility(View.INVISIBLE);
                    Log.d(Constants.TAG,"failed");
                   // Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();
                    et_email.setVisibility(View.GONE);
                    et_code.setVisibility(View.VISIBLE);
                    et_password.setVisibility(View.VISIBLE);
                    tv_timer.setVisibility(View.VISIBLE);
                    btn_reset.setText("Change Password");
                    isResetInitiated = true;
                    startCountdownTimer();
                   // Snackbar.make(getView(), t.getLocalizedMessage(), Snackbar.LENGTH_LONG).show();


                }
            });
        }

        private void finishResetPasswordProcess(String email,String code, String password){
            HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
            interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
            Gson gson = new GsonBuilder()
                           .setLenient()
                           .create();
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(Constants.BASE_URL)
                    .client(client)
                    .addConverterFactory(GsonConverterFactory.create(gson))
                    .build();

            RequestInterface requestInterface = retrofit.create(RequestInterface.class);

            User user = new User();
            user.setEmail(email);
            user.setCode(code);
            user.setPassword(password);
            ServerRequest request = new ServerRequest();
            request.setOperation(Constants.RESET_PASSWORD_FINISH);
            request.setUser(user);
            Call<ServerResponse> response = requestInterface.operation(request);

            response.enqueue(new Callback<ServerResponse>() {
                @Override
                public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {

                    ServerResponse resp = response.body();
                    Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();

                    if(resp.getResult().equals(Constants.SUCCESS)){

                        Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();
                        countDownTimer.cancel();
                        isResetInitiated = false;
                        goToLogin();

                    } else {

                        Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_LONG).show();

                    }
                    progress.setVisibility(View.INVISIBLE);
                }

                @Override
                public void onFailure(Call<ServerResponse> call, Throwable t) {

                    progress.setVisibility(View.INVISIBLE);
                    Log.d(Constants.TAG,"failed");
                   // Snackbar.make(getView(), t.getLocalizedMessage(), Snackbar.LENGTH_LONG).show();
                    countDownTimer.cancel();
                    isResetInitiated = false;
                    goToLogin();

                }
            });
        }
Mayur Gangurde
  • 1,552
  • 12
  • 22
  • 1
    After editing it i got another error -> java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ – Misty Jan 19 '17 at 10:35
  • Give ServerResponse class code also in the question. Now, this is json parsing error. – Mayur Gangurde Jan 19 '17 at 10:43
  • I have Added ServResponse class. – Misty Jan 19 '17 at 10:46
  • 1
    In which function you are getting this error Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ and what's that response – Mayur Gangurde Jan 19 '17 at 11:11
  • private void initiateResetPasswordProcess(String email) – Misty Jan 19 '17 at 11:12
  • And what json response is returning? – Mayur Gangurde Jan 19 '17 at 11:15
  • Json is not returning. I got failed, in my Log.d(Constants.TAG,"failed"); tag. – Misty Jan 19 '17 at 11:17
  • Debug your app by placing breakpoint on that line, and check the value of t of onFailure(Call call, Throwable t). – Mayur Gangurde Jan 19 '17 at 11:21
  • i got it by debuging email = "myemail@gmail.com" this = {ResetPasswordFragment@831728018520} "ResetPasswordFragment{536d322c #0 id=0x7f0c006d}" – Misty Jan 19 '17 at 11:25
  • I think whatever the input needed for API is not satisfying or may be server error. If you are got your asked question's answer, then please, upvote the answer. – Mayur Gangurde Jan 19 '17 at 11:26
  • 1
    Try to check the API by using POSTMAN REST API Client. There you will get to know what input you are entering and the response. Depending on that you will get the idea what things need to change in the code. – Mayur Gangurde Jan 19 '17 at 11:29
  • Can you post input JSON string needed for the API – Mayur Gangurde Jan 19 '17 at 11:37
  • Can you give what input JSON string needed for the API. Also, try by adding @Headers("Content-Type: application/json") in the interface method – Mayur Gangurde Jan 19 '17 at 11:43
  • i have added { "operation":"resPassReq", "user":{ "email":"name@example.com" } } in postman than i got { "result": "success", "message": "Check your mail for reset password code." } – Misty Jan 19 '17 at 11:45
  • i have added @Headers("Content-Type: application/json") but it's not working – Misty Jan 19 '17 at 11:47
  • i have got email from my server but on my app message show failed. – Misty Jan 19 '17 at 11:48
  • Another point is that everything is working on localhost but on my Godaddy server it's now working.. i got JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $ Error – Misty Jan 19 '17 at 11:50
  • Convert your ServerRequest object to string using Gson and print that string in the Log. And for malformed JSON you should set gson setlenient property to true while using GsonConvertorFactory as i mentioned in the answer. – Mayur Gangurde Jan 19 '17 at 11:56
  • Do you understand malformed JSON error solution? And in private void initiateResetPasswordProcess(String email) before Call response = requestInterface.operation(request); Add this code Gson gson = new Gson(); String s = gson.toJson(request); Log.d("request data",s); – Mayur Gangurde Jan 19 '17 at 12:04
  • Not understood malformed JSON ERROR solution – Misty Jan 19 '17 at 12:06
  • For malformed JSON ERROR solution, updated the answer. – Mayur Gangurde Jan 19 '17 at 12:11
  • For another problem, in private void initiateResetPasswordProcess(String email) before Call response = requestInterface.operation(request); Add this code Gson gson = new Gson(); String s = gson.toJson(request); Log.d("request data",s); AND let me know the logs. – Mayur Gangurde Jan 19 '17 at 12:12
  • 1
    Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/133545/discussion-on-answer-by-mdg5435-jsonreader-setlenienttrue-to-accept-malformed). – Bhargav Rao Jan 19 '17 at 12:14
  • Thank you for moving. Looking for it. – Mayur Gangurde Jan 19 '17 at 12:17
  • got error JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $ Error – Misty Jan 19 '17 at 12:21
  • You should really explain your answer in smaller pieces than only provide a block of code – OneCricketeer Jan 19 '17 at 13:01
  • Another Error after put your codes - > java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $ – Misty Jan 21 '17 at 06:48
  • Share your JSON data – Mayur Gangurde Jan 21 '17 at 09:10
  • { "operation":"resPassReq", "user":{ "email":"name@example.com" } } – Misty Jan 21 '17 at 09:12
  • Have you checked Retrofit Logs in the Log trace? May be there you will get to know your API url, passed data. – Mayur Gangurde Jan 21 '17 at 09:30
  • You haven't got the solution for your problem till now ? – Mayur Gangurde Feb 03 '17 at 17:39
  • Is this answer correct? is it accepted? i am facing same issue. and the main problem I am facing is that on my testing device I'm not facing this issue, but when i release it to playstore and check fabric I see 1000 crashes. It is really causing problem. How do i solve this? – Parth Anjaria Oct 03 '18 at 09:29
  • @Parth, while adding GsonConverterFactory to Retrofit's ConverterFactory pass gson object with setLenient property. (Gson gson = new GsonBuilder() .setLenient() .create();) – Mayur Gangurde Oct 09 '18 at 12:39
-2

resolver this error for uploading Image with Retrofit 2 in android:JsonReader.setLenient(true) to accept malformed JSON at line 1 column 1 path $ Error

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode ==10) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType("image/*");
            startActivityForResult(intent.createChooser(intent, "Select Image"), 10);
        } else {
            Toast.makeText(this, "you have dont Permisson", Toast.LENGTH_SHORT).show();
        }
        return;
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}