-1

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!

ciyo
  • 725
  • 4
  • 16
  • 36

3 Answers3

1
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

Community
  • 1
  • 1
FilipPudak
  • 79
  • 9
  • After I added "http://" and made "setText" as comment, it added 2 more lines to LogCat – ciyo May 15 '13 at 22:05
  • `Error parsing data org.json.JSONException: Value – FilipPudak May 15 '13 at 22:11
  • I also use a class named `JSONParser` to get `JSONObject`. Could the error be in it? – ciyo May 15 '13 at 22:16
  • The problem is that you are trying to parse a String which does not contain JSON as JSON. Have a look at what JSON format looks like @ http://www.w3schools.com/json/ What you expose is a html site which cannot be parsed as with a JSON parser but you need to make a HTML parser yourself or find one online – FilipPudak May 16 '13 at 05:19
  • Can solving JSON problem also solve the `FATAL EXCEPTION: AsyncTask #1` problem? – ciyo May 16 '13 at 12:48
1

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.

ciyo
  • 725
  • 4
  • 16
  • 36
0

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

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • After I added "http://" and made "setText" as comment, it added 2 more lines to LogCat: E/JSON Parser(11835): Error parsing data org.json.JSONException: Value – ciyo May 15 '13 at 22:01
  • Please don't change your OP...it will be confusing and useless for others. You can add to it if you are having an issue similar to your original issue. If it is a separate issue then you can accept an answer that helped with the original problem and post a new question – codeMagic May 15 '13 at 22:33
  • As you can see from LogCat FATAL EXCEPTION: AsyncTask #1 is still unsolved. Only error parsing data was added to LogCat and I cannot get an answer to solve the problems. – ciyo May 15 '13 at 22:57
  • I understand that...adding extra logcat was fine. I just meant commenting out the `setText()` calls. I will try to look some more in a bit – codeMagic May 15 '13 at 23:17