簡體   English   中英

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

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

我正在嘗試使用post方法和參數調用php web服務,但是我在OutputStreamWriter處遇到異常wr = new OutputStreamWriter(conn.getOutputStream()); 這行代碼,我已經通過調試注意到了,我已經搜索了此錯誤,但沒有找到任何適當的解決方案,有人可以幫助我解決此錯誤嗎? 提前致謝。

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

在發送參數時,請嘗試以下操作:

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