带有URL调用的API Java和PHP

时间:2016-10-13 03:33:36

标签: java php api

使用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();

1 个答案:

答案 0 :(得分:1)

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();
        }