douhuang4166 2016-07-11 06:19
浏览 44
已采纳

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

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)

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.

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条回答 默认 最新

  • dtkjthe4025 2016-07-11 07:12
    关注
    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));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员