要使用 PHP API Java 代码,并将 Java 前端与将连接到数据库的 PHP 后端连接起来,您只需在代码中调用 PHP 文件的 URL,对吗?
URL url = new URL("http://10.0.3.2/MYCODE/app/login.php");
String urlParams = "name="+name+"&password="+password;
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
OutputStream os = httpURLConnection.getOutputStream();
os.write(urlParams.getBytes());
os.flush();
os.close();
最佳答案
public String login(String name ,String password) {
StringBuilder sURL = new StringBuilder(100);
StringBuilder sb1 = new StringBuilder();
System.out.println("FF:"+pathReader);
try {
sURL.append("http://10.0.3.2/MYCODE/app/login.php?");
sURL.append("name="+name);
sURL.append("&password="+password);
InputStream is = new URL(sURL.toString()).openStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
int cp;
while ((cp = rd.read()) != -1) {
sb1.append((char) cp);
}
}
catch (Exception me) {
System.out.println("## Exception :" + me.getMessage());
}
sb1.toString();
}
关于java - API Java 和 PHP 以及 URL 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40011876/