duanchi8112 2016-07-15 11:12
浏览 500

如何在android中解决Java.Io.IoException

I'm trying to call php web service using post method and parameters, but im getting exception at OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); this line, i have noticed by debugging, i have search for this error but not getting any proper solutions, can anyone help me to solve this error? thanks in advance.

String data = URLEncoder.encode("name", "UTF-8")
                + "=" + URLEncoder.encode("wsd", "UTF-8");

        data += "&" + URLEncoder.encode("email", "UTF-8") + "="
                + URLEncoder.encode("asd", "UTF-8");

        data += "&" + URLEncoder.encode("user", "UTF-8")
                + "=" + URLEncoder.encode("asd", "UTF-8");

        data += "&" + URLEncoder.encode("pass", "UTF-8")
                + "=" + URLEncoder.encode("sad", "UTF-8");

        String text = "";
        BufferedReader reader=null;
        try
        {

            // Defined URL  where to send data
            URL url = new URL("http://androidexample.com/media/webservice/httppost.php");

            // Send POST data request

            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
            wr.write( data );
            wr.flush();

            // Get the server response

            reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line = null;

            // Read Server Response
            while((line = reader.readLine()) != null)
            {
                // Append server response in string
                sb.append(line + "
");
            }


            text = sb.toString();
        }
        catch(Exception ex)
        {

        }
        finally
        {
            try
            {

                reader.close();
            }

            catch(Exception ex) {}
        }

        // Show response on activity
        //content.setText( text  );

    return text;
    }
  • 写回答

1条回答 默认 最新

  • doucan8049 2016-07-15 11:18
    关注

    On sending parameters, try this:

    OutputStream output = new BufferedOutputStream(urlConnection.getOutputStream());
    output.write(param.getBytes());
    output.flush();
    output.close();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作