2016-07-15 111 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

你能發佈異常消息嗎? –

相關問題