我正在尝试使用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;
}
答案 0 :(得分:1)
发送参数时,请尝试以下操作:
OutputStream output = new BufferedOutputStream(urlConnection.getOutputStream());
output.write(param.getBytes());
output.flush();
output.close();