我试图在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();
}