There is a LoginActivity with this code

public class Login extends Activity { public static String URL_LOGIN = "http://mysite.ru/login"; EditText etLogin; EditText etPassword; Button btnLogin; List<NameValuePair> params; SharedPreferences sharedPreferences; ServerRequest serverRequest; ProgressBar pbLogin; Connection connection; @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); serverRequest = new ServerRequest(); pbLogin = (ProgressBar) findViewById(R.id.pbLogin); pbLogin.setVisibility(View.INVISIBLE); etLogin = (EditText) findViewById(R.id.etLogin); etPassword = (EditText) findViewById(R.id.etPassword); btnLogin = (Button) findViewById(R.id.btnLogin); btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pbLogin.setVisibility(View.VISIBLE); String email = etLogin.getText().toString(); String password = etPassword.getText().toString(); params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("email", email)); params.add(new BasicNameValuePair("password", password)); ServerRequest serverRequest = new ServerRequest(); JSONObject json = serverRequest.getJSON(URL_LOGIN, params); if (json != null) { try { String jsonstr = json.getString("response"); if (json.getBoolean("res")) { String token = json.getString("token"); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("token", token); editor.apply(); Intent intent = new Intent(Login.this, Main.class); startActivity(intent); finish(); } Toast.makeText(getApplication(), jsonstr, Toast.LENGTH_LONG).show(); } catch (JSONException e) { e.printStackTrace(); } } } }); } 

And such ServerRequest

 public class ServerRequest { static InputStream is = null; static JSONObject jObj = null; static String json = ""; public ServerRequest() { } public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) { try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line).append("n"); } is.close(); json = sb.toString(); Log.e("JSON", json); } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } try { jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } return jObj; } JSONObject jobj; public JSONObject getJSON(String url, List<NameValuePair> params) { Params param = new Params(url,params); Request myTask = new Request(); try{ jobj= myTask.execute(param).get(); }catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } return jobj; } private static class Params { String url; List<NameValuePair> params; Params(String url, List<NameValuePair> params) { this.url = url; this.params = params; } } private class Request extends AsyncTask<Params, String, JSONObject> { @Override protected JSONObject doInBackground(Params... args) { ServerRequest request = new ServerRequest(); return request.getJSONFromUrl(args[0].url,args[0].params); } @Override protected void onPostExecute(JSONObject json) { super.onPostExecute(json); } } 

}

When connecting to the server, the server returns a token, it can be seen in the Log-ah, but after that errors begin to appear.

  04-10 11:38:50.768 2656-2926/com.dpa7dujijiepgmail.app E/token: {"status":"OK","token":"5de99ea9a1bb550c02a8c2c74e79ac485707dce021cd2b021c8b4567"}n 04-10 11:38:50.768 2656-2656/com.dpa7dujijiepgmail.app W/System.err: org.json.JSONException: No value for response 04-10 11:38:50.772 2656-2656/com.dpa7dujijiepgmail.app W/System.err: at org.json.JSONObject.get(JSONObject.java:354) 04-10 11:38:50.772 2656-2656/com.dpa7dujijiepgmail.app W/System.err: at org.json.JSONObject.getString(JSONObject.java:510) 04-10 11:38:50.772 2656-2656/com.dpa7dujijiepgmail.app W/System.err: at com.dpa7dujijiepgmail.app.Login$1.onClick(Login.java:67) 04-10 11:38:50.772 2656-2656/com.dpa7dujijiepgmail.app W/System.err: at android.view.View.performClick(View.java:4084) 04-10 11:38:50.772 2656-2656/com.dpa7dujijiepgmail.app W/System.err: at android.view.View$PerformClick.run(View.java:16966) 04-10 11:38:50.772 2656-2656/com.dpa7dujijiepgmail.app W/System.err: at android.os.Handler.handleCallback(Handler.java:615) 04-10 11:38:50.772 2656-2656/com.dpa7dujijiepgmail.app W/System.err: at android.os.Handler.dispatchMessage(Handler.java:92) 04-10 11:38:50.772 2656-2656/com.dpa7dujijiepgmail.app W/System.err: at android.os.Looper.loop(Looper.java:137) 04-10 11:38:50.772 2656-2656/com.dpa7dujijiepgmail.app W/System.err: at android.app.ActivityThread.main(ActivityThread.java:4745) 04-10 11:38:50.772 2656-2656/com.dpa7dujijiepgmail.app W/System.err: at java.lang.reflect.Method.invokeNative(Native Method) 04-10 11:38:50.772 2656-2656/com.dpa7dujijiepgmail.app W/System.err: at java.lang.reflect.Method.invoke(Method.java:511) 04-10 11:38:50.772 2656-2656/com.dpa7dujijiepgmail.app W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 04-10 11:38:50.772 2656-2656/com.dpa7dujijiepgmail.app W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 04-10 11:38:50.772 2656-2656/com.dpa7dujijiepgmail.app W/System.err: at dalvik.system.NativeStart.main(Native Method) 

I can not figure out this problem, what is wrong here?

    1 answer 1

    Apparently you expect json this format

     {"status":"OK","token":"5de99ea9a1bb550c02a8c2c74e79ac485707dce021cd2b021c8b4567"} 

    and in the code you are trying to do so

      String jsonstr = json.getString("response"); 

    in your json there is no key "response", and there are logs in the console because of this, a similar situation will be for

     json.getBoolean("res") 

    You first need to make sure whether there is such a key in json

     if (jsonObject.has("response")) { //тогда получаем значение по этому ключу }