简体   繁体   English

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

[英]how to solve Java.Io.IoException in android

I'm trying to call php web service using post method and parameters, but im getting exception at OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 我正在尝试使用post方法和参数调用php web服务,但是我在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 + "\n");
            }


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

        }
        finally
        {
            try
            {

                reader.close();
            }

            catch(Exception ex) {}
        }

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

    return text;
    }

On sending parameters, try this: 在发送参数时,请尝试以下操作:

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

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

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