简体   繁体   English

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

[英]JSON data cannot be retrieved in android from php

I want to get the details of the user who has logged in. I am getting null or no value in the alert dialog (var - name) 我想获取已登录用户的详细信息。我在警报对话框中得到null或无值(变量-名称)

I am new to everything so I am sorry if the question is not worth it, I have tried finding the solutions but no luck yet so please help. 我对所有事物都是陌生的,所以很抱歉,如果这个问题不值得,我已经尝试找到解决方案,但是还没有运气,请提供帮助。

在此处输入图片说明

在此处输入图片说明

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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