无法在php中从android中检索JSON数据

时间:2016-07-11 06:19:33

标签: php android mysql json

我想了解已登录用户的详细信息。 我在警告对话框(var-name)

中得到null或没有值

我是新手,所以如果问题不值得我很抱歉,我已经尝试找到解决方案,但没有运气,所以请帮忙。

enter image description here

enter image description here

if (type.equals("linkuser")) {
        try {
            URL url = new URL(Link_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            String post_data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(user, "UTF-8");
            bufferedWriter.write(post_data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
            String result = "";
            String line = "";
            while ((line = bufferedReader.readLine()) != null) {
                result += line;
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return result;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

1 个答案:

答案 0 :(得分:0)

Your php query is wrong
Initialize array first Note that $result is not your array

$menus= array();
while($row = mysqli_fetch_array($result )){
array_push($menus,
array('name'=>$row[0],
'ign'=>$row[1],
'email'=>$row[2]
));
}

mysqli_close($con);

echo json_encode(array("menu"=>$menus));