Question

I am newbie on Android and I am developing an app that gets some information from a QR code and then connects a php page. However app crushes after it reads QR code and logCat gives a Fatal Exception AsyncTask #1 when I run it.

Here is the doInBackground() codes:

class CreateNewProduct extends AsyncTask<String, String, String>
    { 
        protected String doInBackground(String... args)
        {
            String tc= new String();
            tc= "3123";

            String no = new String();
            no = "3";

            /*String tcKimlik = "3123";
            String hatNo = "3";*/

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("tc", tc));
            params.add(new BasicNameValuePair("no", no ));

            JSONObject json = jsonParser.makeHttpRequest(url_create_product, "POST", params);

            try
            {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1)
                {

                    //qrCodeText.setText("DONE!");
                }
                else
                {
                    //qrCodeText.setText("Nope...");
                }
            }
            catch (JSONException e)
            {
                e.printStackTrace();
            }

            return null;
        }

    } // End of CreateNewProduct

CreateNewProduct is in another class that reads QR codes and I call it like new CreateNewProduct().execute(); after Qr code is read.

And here is LogCat:

UPDATE:

After I added http:// and made setTexT() as comment, JSON error was added to LogCat:

05-16 01:00:08.900: E/JSON Parser(11835): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

05-16 01:00:08.900: W/dalvikvm(11835): threadid=14: thread exiting with uncaught exception (group=0x410572a0)
05-16 01:00:08.910: E/AndroidRuntime(11835): FATAL EXCEPTION: AsyncTask #1
05-16 01:00:08.910: E/AndroidRuntime(11835): java.lang.RuntimeException: An error occured while executing doInBackground()
05-16 01:00:08.910: E/AndroidRuntime(11835):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-16 01:00:08.910: E/AndroidRuntime(11835):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
05-16 01:00:08.910: E/AndroidRuntime(11835):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
05-16 01:00:08.910: E/AndroidRuntime(11835):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
05-16 01:00:08.910: E/AndroidRuntime(11835):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-16 01:00:08.910: E/AndroidRuntime(11835):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
05-16 01:00:08.910: E/AndroidRuntime(11835):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
05-16 01:00:08.910: E/AndroidRuntime(11835):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
05-16 01:00:08.910: E/AndroidRuntime(11835):    at java.lang.Thread.run(Thread.java:856)
05-16 01:00:08.910: E/AndroidRuntime(11835): Caused by: java.lang.NullPointerException
05-16 01:00:08.910: E/AndroidRuntime(11835):    at com.example.yoritreader.MainActivity$CreateNewProduct.doInBackground(MainActivity.java:442)
05-16 01:00:08.910: E/AndroidRuntime(11835):    at com.example.yoritreader.MainActivity$CreateNewProduct.doInBackground(MainActivity.java:1)
05-16 01:00:08.910: E/AndroidRuntime(11835):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-16 01:00:08.910: E/AndroidRuntime(11835):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-16 01:00:08.910: E/AndroidRuntime(11835):    ... 5 more

Thank you for your help!

Was it helpful?

Solution 2

The error was solved removing <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> from php file.

OTHER TIPS

Target host must not be null, or set in parameters. scheme=null, host=null, path=www.halilcosgun.com/qrcode.php

You probably forgot to put the protocol in the URL of your site.

Try with http://www.halilcosgun.com/qrcode.php

https://stackoverflow.com/a/8768930/1296658

I believe your current problem is that you need to add "http://" or the like to your url when you send the request. That should take care of your current problem, IllegalStateException. Your next problem is going to be trying to access UI elements from a background Thread. You need to move the lines where you call setText() to onPostExecute() or to an Activity method

Since you aren't allowed to access UI elements from the background this line

qrCodeText.setText("DONE!");

and any others where you try to update the UI will give you an error. You can return success to onPostExecute() and perform it that way since all other AsyncTask methods run on the UI. This is assuming they have access to these elements as in being an inner class of the Activity or passing a reference to them in your constructor

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top