0

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

I get the following error: java.io.FileNotFoundException:

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

1
  • if you not interested to respond to the answer then don't post the questions. Commented Dec 9, 2016 at 12:04

3 Answers 3

3

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;
    }`
Sign up to request clarification or add additional context in comments.

Comments

1

Please ensure the following 2 lines in Manifest and WIFI is enabled and connected to subnet 10.0.3.x

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

If post method is get, where is "?" in your URL?

Comments

0

you can use in this methodURL url = new URL("http://localhost/test.php?print=success"); URLConnection connection = url.openConnection(); connection.connect();

same this i have use for one of my project this cluesshop.com

1 Comment

I don't see how this answers the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.