我曾尝试在localhost上连接一个php脚本,但如果没有inputstream,就无法访问该脚本。我无意中使代码工作,但不知道为什么它是工作在有inputstream,但不是在没有inputstream
try {
String url= "10.0.2.2/arrival.php";
URL loginurl= new URL(url);
HttpURLConnection http= (HttpURLConnection)loginurl.openConnection();
http.setRequestMethod("POST");
http.setDoOutput(true);
http.setDoInput(true);
OutputStream out= http.getOutputStream();
BufferedWriter bfwr= new BufferedWriter(new OutputStreamWriter(out,"UTF-8"));
String post_data= URLEncoder.encode("renter_id","UTF-8")+"="+URLEncoder.encode(renter_id,"UTF-8");
bfwr.write(post_data);
bfwr.flush();
out.close();
bfwr.close();
InputStream inpt= http.getInputStream();
BufferedReader bfrdr= new BufferedReader(new InputStreamReader(inpt,"iso-8859-1"));
String line="";
String result="";
while((line=bfrdr.readLine())!=null){
result+=line;
}
bfrdr.close();
inpt.close();
http.disconnect();
return "";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
暂无答案!
目前还没有任何答案,快来回答吧!