从Android中的PHP后端获取JSON响应

时间:2019-02-05 09:54:39

标签: java php android jsonresponse

我正在向php服务器发送一些数据,并执行一些database操作,并在数据库中进行了json操作之后向Android应用返回了insert数据

数据库处理成功,但是我无法在android中获取json响应! 我在下面的代码中使用AsyncTask。

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

2 个答案:

答案 0 :(得分:0)

  

如果找不到密钥,则getString()方法将引发异常。而是使用has()方法。

删除日志语句Log.i("json", json.getString("insert_sucss"));或更改为json.has("insert_sucss")

如果您仅发送2个错误代码(insert_sucss和insert_err),请将if条件更改为类似的内容

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

否则将涵盖所有此类可能性

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

答案 1 :(得分:0)

我发现了这个问题 我是从工作线程调用的,这是错误的

我修复了代码

public List<Person> parse(final InputStream is) throws IOException {
    CharSet cs = ... // Use the right charset for your file
    Stream<String> lines = new BufferedReader(new InputStreamReader(is, cs)).lines();
    return  lines
            .map(Person::new)   
            .collect(toList());
}