Reputation: 41
To API Java code with PHP, and connect a Java frontend with a PHP backend that will connect to a database, you just call the PHP file's URL in the code, correct?
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();
Upvotes: 0
Views: 437
Reputation: 805
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();
}
Upvotes: 1