Hi, I am throwing a request to get a token in OAuth VK :

JSONObject parsed_url = parseURL("https://oauth.vk.com/token" + "?grant_type=password" + "&client_id=*********" + "&client_secret=*********" + "&scope=status,messages" + "&username=" + login + "&password=" + password ); 

and if I enter the correct data (login and password) - it gives me a JSON object with which I work, and if I send non-valid data, then FileNotFoundExpection throws FileNotFoundExpection , I would like to know why and how to get rid of it, it does not allow me to send Toast with a message about incorrectly entered data.

Other codes:

 public JSONObject parseURL(String url_string) throws IOException, JSONException{ enableStrictMode(); InputStream is = new URL(url_string).openStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String jsonText = readAll(rd); JSONObject json = new JSONObject(jsonText); is.close(); return json; } View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { int id = v.getId(); if (id == R.id.login_button) { if (validateData()) { try { login(); } catch (Exception e) { e.printStackTrace(); } } } } }; login_button.setOnClickListener(listener); 

}

Not a fan of libraries, so I do not use them.

All good, if something is not explained - forgive.

Mistake:

 08-28 22:34:53.491 5615-5615/ru.steelskype.app W/System.err: java.io.FileNotFoundException: https://oauth.vk.com/token?grant_type=password&client_id=*****&client_secret=*****&scope=status,messages&username=ususjsjsj&password=hshshshshs 08-28 22:34:53.498 5615-5615/ru.steelskype.app W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:238) 08-28 22:34:53.498 5615-5615/ru.steelskype.app W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210) 08-28 22:34:53.498 5615-5615/ru.steelskype.app W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:25) 08-28 22:34:53.498 5615-5615/ru.steelskype.app W/System.err: at java.net.URL.openStream(URL.java:470) 08-28 22:34:53.498 5615-5615/ru.steelskype.app W/System.err: at ru.steelskype.app.LoginActivity.parseURL(LoginActivity.java:100) 08-28 22:34:53.498 5615-5615/ru.steelskype.app W/System.err: at ru.steelskype.app.LoginActivity.login(LoginActivity.java:80) 08-28 22:34:53.498 5615-5615/ru.steelskype.app W/System.err: at ru.steelskype.app.LoginActivity$1.onClick(LoginActivity.java:40) 08-28 22:34:53.498 5615-5615/ru.steelskype.app W/System.err: at android.view.View.performClick(View.java:5207) 08-28 22:34:53.498 5615-5615/ru.steelskype.app W/System.err: at android.view.View$PerformClick.run(View.java:21177) 08-28 22:34:53.498 5615-5615/ru.steelskype.app W/System.err: at android.os.Handler.handleCallback(Handler.java:739) 08-28 22:34:53.498 5615-5615/ru.steelskype.app W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95) 08-28 22:34:53.498 5615-5615/ru.steelskype.app W/System.err: at android.os.Looper.loop(Looper.java:148) 08-28 22:34:53.498 5615-5615/ru.steelskype.app W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5441) 08-28 22:34:53.498 5615-5615/ru.steelskype.app W/System.err: at java.lang.reflect.Method.invoke(Native Method) 08-28 22:34:53.498 5615-5615/ru.steelskype.app W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738) 08-28 22:34:53.498 5615-5615/ru.steelskype.app W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628) 
  • {"error": "invalid_client", "error_description": "Username or password is incorrect"} - if entered from a browser or in PHP - xISRAPILx
  • This is actually Java - zuvladimir
  • Yes, I know, but what does it have to do with it? I brought this, so that you would be convinced that the browser gives a normal error in JSON. - xISRAPILx
  • I would advise the option of sending a request using HttpUrlConnection as it says here mkyong.com/java/how-to-send-http-request-getpost-in-java . The log shows that the resource was not found, JSON may be somehow incorrectly generated in case of invalid data - zuvladimir
  • @zuvladimir json works fine and parses in PHP, but does not want to here. - xISRAPILx

1 answer 1

With invalid data, the server returns status 401, android com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream translates it into a FileNotFoundExpection exception. You can handle the exception in try-catch, or try using another HTTP Client to achieve your goal.

  • Yes, I already did (try \ catch), but it seems to me a silly crutch. - xISRAPILx