-2

I am sending some data to php server and performing some database operations and returns a json data to android app after insert operations in database

Database process is successful but i can't get the json response in android! I am using AsyncTask in this code below.

try {
            //connect to php and send data code...

            HttpResponse response = httpclient.execute(httppost);
            InputStream inputStream = response.getEntity().getContent();
            String result = convertInputStreamToString(inputStream);

            if (result != null) {
                try {

                    JSONObject json = new JSONObject(result);
                    Log.i("json", json.getString("insert_sucss"));
                    if (json.has("insert_err")) {
                        Toast.makeText(RegisterActivity.this, json.getString("insert_err"), Toast.LENGTH_LONG).show();
                    }
                    if (json.has("insert_sucss")) {
                        Toast.makeText(RegisterActivity.this, json.getString("insert_sucss"), Toast.LENGTH_LONG).show();
                    }
                }
                catch (JSONException e)
                {
                    e.printStackTrace();
                }

            }
        }
        catch (Exception e)
        {
            Log.e("log_tag", "Error:  " + e.toString());
        }
        return null;
    }


    private String convertInputStreamToString(InputStream inputStream) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder builder = new StringBuilder();

            String line = "";

            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }

            return builder.toString();
        }
        catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
ankuranurag2
  • 2,300
  • 15
  • 30
kamal
  • 330
  • 3
  • 13

2 Answers2

0

The getString() method throws exception if key not found. Instead use the has() method.

Remove the log statement Log.i("json", json.getString("insert_sucss")); or change to json.has("insert_sucss")

Also if you're only sending 2 error codes (insert_sucss and insert_err) change the if conditions to something like this

if (json.has("insert_err")) 
{
      Toast.makeText(RegisterActivity.this, json.getString("insert_err"), Toast.LENGTH_LONG).show();
}
else 
{
      Toast.makeText(RegisterActivity.this, json.getString("insert_sucss"), Toast.LENGTH_LONG).show();
}

otherwise cover all possibilities like this

if (json.has("insert_err")) 
{
      Toast.makeText(RegisterActivity.this, json.getString("insert_err"), Toast.LENGTH_LONG).show();
}
else if(json.has("insert_sucss"))
{
      Toast.makeText(RegisterActivity.this, json.getString("insert_sucss"), Toast.LENGTH_LONG).show();
}
else
{
      Toast.makeText(RegisterActivity.this, "Unexpected Error", Toast.LENGTH_LONG).show();
}
abhndv
  • 209
  • 4
  • 13
  • in toast , second parameter should be text!! – kamal Feb 05 '19 at 13:41
  • oooh my bad. accidentally replaced all `json.getString()` to `json.has()`. i'll edit it – abhndv Feb 05 '19 at 13:49
  • `Log.i("json", json.getString("insert_sucss"));` this log msg will be triggering the exception if there is not a insert_sucss keyword. Use Don't use getString unless you are sure that it is there – abhndv Feb 05 '19 at 13:57
  • The code i gave then was just to improve your code. My answer is the highlighted one. – abhndv Feb 05 '19 at 13:58
0

I found this problem I am calling it from a worker thread.and this is wrong

I fixed my code

 runOnUiThread(new Runnable() {

                            public void run() {
                                Toast.makeText(G.context, "text", Toast.LENGTH_LONG).show();
                            }
                        });
kamal
  • 330
  • 3
  • 13