1

I have an issue on my Android Project,

I want to call php page with get method and I use HttpURLConnection, when I run this application give an error "app has stopped"

This is my code

    public void someFunction() throws IOException {

            String inputLine = "";

    URL url = new URL("http://www.domain.com/demo.php?test=abc");
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            InputStream in = new BufferedInputStream(urlConnection.getInputStream());
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                StringBuilder result = new StringBuilder();
                String line;
                while((line = reader.readLine()) != null) {
                    result.append(line);
                }

            } finally{
                    in.close();
                }
}

What should I do?

Thanks.

4
  • If you use catch instead of throw then you can always print the exception trace and know the reason of App exit. Commented Feb 24, 2015 at 1:01
  • let me guess, yet another NOMTE's question Commented Feb 24, 2015 at 1:05
  • NOMTE = NetworkOnMainThreadException Commented Feb 24, 2015 at 1:13
  • I have solved "Strict Mode" way. Thanks for help Commented Feb 24, 2015 at 2:05

2 Answers 2

3

I have solved this issue with following code,

 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
Sign up to request clarification or add additional context in comments.

Comments

0

Android is build to resist the developers to make network requests in the main (UI) thread. Changing Thread policy is not the solution. You should use AsyncTask to make all network requests.

public void someFunction() throws IOException {

    AsyncTask task = new AsyncTask<Void,StringBuilder,StringBuilder>() {
        @Override
        protected StringBuilder doInBackground(Void... params) {

String inputLine = "";

URL url = new URL("http://www.domain.com/demo.php?test=abc");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuilder result = new StringBuilder();
            String line;
            while((line = reader.readLine()) != null) {
                result.append(line);
            }

        } finally{
                in.close();
            }

            return result;
        }


        @Override
        protected void onPostExecute(StringBuilder result) {
            super.onPostExecute(o);

        }
    };
    task.execute();

}

See the android documentation: http://developer.android.com/reference/android/os/AsyncTask.html

Comments

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.