JSON parsing for sending verification code from server to mail is not working

290 views Asked by At

Consider the code snippet :

        @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        emailto= (EditText)findViewById(R.id.editText3);
        verification = (EditText) findViewById( R.id.editText4);
        username = (EditText)findViewById( R.id.editText1);
        password= ( EditText) findViewById(R.id.editText2);
        facebook = (EditText)findViewById( R.id.editText5);
        twitter = ( EditText) findViewById(R.id.editText6);


        verification.setOnFocusChangeListener(new OnFocusChangeListener()
        {
            @Override
            public void onFocusChange(View v, boolean hasFocus) 
            {                   
                new CheckLoginStatusBackgroundThread().execute("");
            }
            });

    }


        class CheckLoginStatusBackgroundThread extends
            AsyncTask<String, Void, String> {




        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... params) {

         HttpURLConnection linkConnection = null;
          try {
            URL linkurl = new URL("http://iipacademy.inaskpoll?email="+emailto);
                linkConnection = HttpURLConnection)linkurl.openConnection();
                int responseCode = linkConnection.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                  InputStream linkinStream =linkConnection.getInputStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    int j = 0;
                    while ((j = linkinStream.read()) != -1) {
                        baos.write(j);
                    }
                    byte[] data = baos.toByteArray();
                    jsonString = new String(data);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (linkConnection != null) {
                    linkConnection.disconnect();
                }
            }
            Toast.makeText(getApplicationContext(), jsonString.toString(),
                    Toast.LENGTH_SHORT).show();
            return jsonString;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
        code = result.toString();
                 }
    }

I want that this code should send an auto-generated code to my mail id provided in EditText-'emailto' . This script- "http://iipacademy.in/askpoll/?email="+emailto
is working fine(as seen that it is working perfectly in browser) & is sending email to my email id.

means hat there is no problem in my script whereas the problem in in my android code snippet provided above.

I tried much but could not find the fault

thanks...

UPDATE:

12-03 12:37:45.040: E/AndroidRuntime(2496): FATAL EXCEPTION: AsyncTask #2
12-03 12:37:45.040: E/AndroidRuntime(2496): java.lang.RuntimeException: An error occured while executing doInBackground()
12-03 12:37:45.040: E/AndroidRuntime(2496):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-03 12:37:45.040: E/AndroidRuntime(2496):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
12-03 12:37:45.040: E/AndroidRuntime(2496):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
12-03 12:37:45.040: E/AndroidRuntime(2496):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
12-03 12:37:45.040: E/AndroidRuntime(2496):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-03 12:37:45.040: E/AndroidRuntime(2496):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-03 12:37:45.040: E/AndroidRuntime(2496):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-03 12:37:45.040: E/AndroidRuntime(2496):     at java.lang.Thread.run(Thread.java:841)
12-03 12:37:45.040: E/AndroidRuntime(2496): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
12-03 12:37:45.040: E/AndroidRuntime(2496):     at android.os.Handler.<init>(Handler.java:197)
12-03 12:37:45.040: E/AndroidRuntime(2496):     at android.os.Handler.<init>(Handler.java:111)
12-03 12:37:45.040: E/AndroidRuntime(2496):     at android.widget.Toast$TN.<init>(Toast.java:324)
12-03 12:37:45.040: E/AndroidRuntime(2496):     at android.widget.Toast.<init>(Toast.java:91)
12-03 12:37:45.040: E/AndroidRuntime(2496):     at android.widget.Toast.makeText(Toast.java:238)
12-03 12:37:45.040: E/AndroidRuntime(2496):     at com.example.askpollie.Register$CheckLoginStatusBackgroundThread.doInBackground(Register.java:112)
12-03 12:37:45.040: E/AndroidRuntime(2496):     at com.example.askpollie.Register$CheckLoginStatusBackgroundThread.doInBackground(Register.java:1)
12-03 12:37:45.040: E/AndroidRuntime(2496):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-03 12:37:45.040: E/AndroidRuntime(2496):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-03 12:37:45.040: E/AndroidRuntime(2496):     ... 4 more
12-03 12:37:45.270: W/EGL_emulation(2496): eglSurfaceAttrib not implemented
1

There are 1 answers

0
1615903 On BEST ANSWER

This line is wrong:

URL linkurl = new URL("http://iipacademy.inaskpoll?email="+emailto);

emailto seems to be the EditText object, not the text that you have written to it. Use the getText() method to get the actual text:

String email = emailto.getText().toString();
URL linkurl = new URL("http://iipacademy.inaskpoll?email="+email);