2016-07-15 101 views
0

Java.Io.IoException我想要调用php web服务使用post方法和参数,但即时获取异常在OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());这一行,我已经注意到通过调试,我有搜索这个错误,但没有得到任何适当的解决方案,任何人都可以帮助我解决这个错误?提前致谢。如何解决在android

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

回答

1

在发送参数,试试这个:

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

我得到了同样的错误。 –

+0

你能发布异常消息吗? –