通过URL在Java中调用PHP文件 [英] Calling PHP File in Java via URL

查看:58
本文介绍了通过URL在Java中调用PHP文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个Android应用程序,该应用程序调用php文件以从数据库查询和提取数据.该URL在我的手机上可以通过Web浏览器访问,但是我似乎无法从下面的Java代码中调用它.谁能帮助我从Java代码调用PHP文件.

I'm trying to develop an android app that calls a php file to query and pull data from a database. The URL is accesible on my mobile phone on the web browser, but I can't seem to call it from my java code below. Could anyone assist me in calling my PHP file from my Java code.

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

            InputStream is = httpURLConnection.getInputStream();
            while((tmp=is.read())!=-1){
                data+= (char)tmp;
            }

            is.close();
            httpURLConnection.disconnect();

我收到以下错误: java.io.FileNotFoundException:

我正在使用 POST 方法,因为它更安全.

I'm using the POST method because it's more secure.

推荐答案

如果是GET方法,那么我们可以使用下面的代码行,调用文件的问题可能是您的.php文件路径不正确

If it's GET method then we can use the below lines of code and the issue with your calling file might be your .php file path is incorrect

    //Integrating url with values [Starts]
    Map<String, String> request = new HashMap<String, String>();
    request.put("name", name);
    request.put("password", password);
    Uri.Builder uriBuilder = new Uri.Builder();
    uriBuilder.encodedPath("http://10.0.3.2/MYCODE/app/login.php");
    if (mapOfStrings != null) {
        for (Map.Entry<String, String> entry : request.entrySet()) {
            Log.d("buildSanitizedRequest", "key: " + entry.getKey()
                    + " value: " + entry.getValue());
            uriBuilder.appendQueryParameter(entry.getKey(),
                    entry.getValue());
        }
    }
    String uriString;
    try {
        uriString = uriBuilder.build().toString(); // May throw an
                                                   // UnsupportedOperationException
    } catch (Exception e) {
        Log.e("Exception", "Exception" + e);
    }
    //Integrating url with values [Ends]
    HttpURLConnection connection = null;
    try {
        URL url = new URLuriBuilder.build().toString());
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Accept-Charset", "utf-8,*");
        Log.d("Get-Request", url.toString());
        try {
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(connection.getInputStream()));
            StringBuilder stringBuilder = new StringBuilder();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line).append("\n");
            }
            bufferedReader.close();
            Log.d("Get-Response", stringBuilder.toString());
            return new JSONObject(stringBuilder.toString());
        } finally {
            connection.disconnect();
        }
    } catch (Exception e) {
        Log.e("ERROR", e.getMessage(), e);
        return null;
    }`

这篇关于通过URL在Java中调用PHP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆