I have an application for android which should login on the server for further work. So for this, I created an interface with this code:

import retrofit2.Call; import retrofit2.http.Field; import retrofit2.http.FormUrlEncoded; import retrofit2.http.POST; public interface APIService { Call<LoginRequest> savePost(@Field("id") Integer id, @Field("password") String password); } 

and I have two fields for input where I have to enter a login and password, the data from the fields are transmitted to the server in json format, and my problem is that when I send to the server I get an error which is that the data in the login field is Currently being sent to an int and I cannot translate this data from the string format into an int:

 Integer id = Integer.valueOf((mEmailView.getText().toString().trim())); String password = mPasswordView.getText().toString().trim(); 

and my application flies all the time because an error in this field when translating from one format to another, and therefore the application crashes in me, I kind of looked to translate normally, but for some reason it does not work correctly. Mistake:

  --------- beginning of crash 07-11 02:30:18.411 6910-6910/com.example.developer_4.test_log E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.developer_4.test_log, PID: 6910 java.lang.NumberFormatException: For input string: "jdnjsnj" at java.lang.Integer.parseInt(Integer.java:521) at java.lang.Integer.valueOf(Integer.java:611) at com.example.developer_4.test_log.LoginActivity$2.onClick(LoginActivity.java:105) at android.view.View.performClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) --------- beginning of system 

If someone faced a similar problem or knows how to help me, I will be very grateful.

  • stacktrace where? - rjhdby

1 answer 1

java.lang.NumberFormatException indicates an error when parsing a number from a string. The string you passed is jdnjsnj .

And here is where you do it:

 Integer id = Integer.valueOf((mEmailView.getText().toString().trim())); 

This means that not only numbers are entered into your mEmailView .

Either prohibit the input of any characters except numbers in your input field, or add manual validation before parsing.


I noticed another error in your code:

You use @POST("/{login}/{password}") , but do not write @Path("login") and @Path("password") . Read examples: http://square.imtqy.com/retrofit/

  • and how to make a ban on the input of characters except numbers? - Andrew Goroshko
  • @AndrewGoroshko added the answer. - Peter Samokhin
  • By the way, I’m entering numbers into edittext, I’m not typing in characters at all, and I still have this problem, that is, I still need to insert this prohibition or the problem is in something else ??? - Andrew Goroshko
  • @AndrewGoroshko then you have another problem. In your question, the problem was described, you brought the error error - if you would add only numbers to the input field, but the application still crashes, then another error appears. I gave an exhaustive answer to the current question, check out the stackraces and you can ask a separate question if the error is different. - Peter Samokhin
  • @AndrewGoroshko added an answer, saw another mistake with you. - Peter Samokhin