简体   繁体   中英

Get JSON response from php backend in android

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;
    }

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();
}

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();
                            }
                        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM