android login getting errors with async task
<p>Hi Im trying to use the Async task to utilize a basic login system I got from a tutorial (to just learn). Im having issues though. To get over the android.os.networkexception I use async but I'm sure I'm using it completely incorrectly in this situation. Here is the code:</p>

<pre><code>package com.example.toknapp;

import java.util.ArrayList;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class login2 extends Activity {
    EditText un;
    TextView error;
    Button ok;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        un=(EditText)findViewById(R.id.et_un);
        ok=(Button)findViewById(R.id.btn_login);
        error=(TextView)findViewById(R.id.tv_error);

        ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                GetData task = new GetData();
                task.execute();


            }
        });
    }

    private class GetData extends AsyncTask&lt;String, Void, String&gt; {
        @Override
        protected String doInBackground(String... urls) {
            ArrayList&lt;NameValuePair&gt; postParameters = new ArrayList&lt;NameValuePair&gt;();
            postParameters.add(new BasicNameValuePair("username", un.getText().toString()));
            //String valid = "1";
            String response = null;
            try {
                response = CustomHttpClient.executeHttpPost("http://tokn.me/android_merchant_login.php", postParameters);
                String res=response.toString();
               // res = res.trim();
                res= res.replaceAll("\\s+","");                              
                //error.setText(res);

               if(res.equals("1"))
                    error.setText("Correct Username or Password");
                else
                    error.setText("Sorry!! Incorrect Username or Password"); 
            } catch (Exception e) {
                un.setText(e.toString());
            }


            return response;
        }
    }
}
</code></pre>

<p>logcat:</p>

<pre><code>E/AndroidRuntime( 1188): FATAL EXCEPTION: AsyncTask #1
E/AndroidRuntime( 1188): java.lang.RuntimeException: An error occured while exec
uting doInBackground()
E/AndroidRuntime( 1188):        at android.os.AsyncTask$3.done(AsyncTask.java:27
8)
E/AndroidRuntime( 1188):        at java.util.concurrent.FutureTask$Sync.innerSet
Exception(FutureTask.java:273)
E/AndroidRuntime( 1188):        at java.util.concurrent.FutureTask.setException(
FutureTask.java:124)
E/AndroidRuntime( 1188):        at java.util.concurrent.FutureTask$Sync.innerRun
(FutureTask.java:307)
E/AndroidRuntime( 1188):        at java.util.concurrent.FutureTask.run(FutureTas
k.java:137)
E/AndroidRuntime( 1188):        at android.os.AsyncTask$SerialExecutor$1.run(Asy
ncTask.java:208)
E/AndroidRuntime( 1188):        at java.util.concurrent.ThreadPoolExecutor.runWo
rker(ThreadPoolExecutor.java:1076)
E/AndroidRuntime( 1188):        at java.util.concurrent.ThreadPoolExecutor$Worke
r.run(ThreadPoolExecutor.java:569)
E/AndroidRuntime( 1188):        at java.lang.Thread.run(Thread.java:856)
E/AndroidRuntime( 1188): Caused by: android.view.ViewRootImpl$CalledFromWrongThr
eadException: Only the original thread that created a view hierarchy can touch i
ts views.
E/AndroidRuntime( 1188):        at android.view.ViewRootImpl.checkThread(ViewRoo
tImpl.java:3903)
E/AndroidRuntime( 1188):        at android.view.ViewRootImpl.invalidateChild(Vie
wRootImpl.java:708)
E/AndroidRuntime( 1188):        at android.view.ViewRootImpl.invalidateChildInPa
rent(ViewRootImpl.java:757)
E/AndroidRuntime( 1188):        at android.view.ViewGroup.invalidateChild(ViewGr
oup.java:4006)
E/AndroidRuntime( 1188):        at android.view.View.invalidate(View.java:8432)
E/AndroidRuntime( 1188):        at android.widget.TextView.invalidateCursor(Text
View.java:4318)
E/AndroidRuntime( 1188):        at android.widget.TextView.spanChange(TextView.j
ava:7669)
E/AndroidRuntime( 1188):        at android.widget.TextView$ChangeWatcher.onSpanA
dded(TextView.java:8018)
E/AndroidRuntime( 1188):        at android.text.SpannableStringBuilder.sendSpanA
dded(SpannableStringBuilder.java:898)
E/AndroidRuntime( 1188):        at android.text.SpannableStringBuilder.setSpan(S
pannableStringBuilder.java:614)
E/AndroidRuntime( 1188):        at android.text.SpannableStringBuilder.setSpan(S
pannableStringBuilder.java:520)
E/AndroidRuntime( 1188):        at android.text.Selection.setSelection(Selection
.java:76)
E/AndroidRuntime( 1188):        at android.text.Selection.setSelection(Selection
.java:87)
E/AndroidRuntime( 1188):        at android.text.method.ArrowKeyMovementMethod.in
itialize(ArrowKeyMovementMethod.java:302)
E/AndroidRuntime( 1188):        at android.widget.TextView.setText(TextView.java
:3243)
E/AndroidRuntime( 1188):        at android.widget.TextView.setText(TextView.java
:3109)
E/AndroidRuntime( 1188):        at android.widget.EditText.setText(EditText.java
:78)
E/AndroidRuntime( 1188):        at android.widget.TextView.setText(TextView.java
:3084)
E/AndroidRuntime( 1188):        at com.example.toknapp.login2$GetData.doInBackgr
ound(login2.java:61)
E/AndroidRuntime( 1188):        at com.example.toknapp.login2$GetData.doInBackgr
ound(login2.java:1)
E/AndroidRuntime( 1188):        at android.os.AsyncTask$2.call(AsyncTask.java:26
4)
E/AndroidRuntime( 1188):        at java.util.concurrent.FutureTask$Sync.innerRun
(FutureTask.java:305)
E/AndroidRuntime( 1188):        ... 5 more
W/ActivityManager(   85):   Force finishing activity com.example.toknapp/.toknap
p
W/WindowManager(   85): Failure taking screenshot for (180x300) to layer 21015
W/InputManagerService(   85): Starting input on non-focused client com.android.i
nternal.view.IInputMethodClient$Stub$Proxy@417c3938 (uid=10040 pid=1188)
W/IInputConnectionWrapper( 1188): showStatusIcon on inactive InputConnection
W/NetworkManagementSocketTagger(   85): setKernelCountSet(10004, 1) failed with
errno -2
W/NetworkManagementSocketTagger(   85): setKernelCountSet(10040, 0) failed with
errno -2
I/Process ( 1188): Sending signal. PID: 1188 SIG: 9
W/InputManagerService(   85): Window already focused, ignoring focus gain of: co
m.android.internal.view.IInputMethodClient$Stub$Proxy@4135b3d8
I/ActivityManager(   85): Process com.example.toknapp (pid 1188) has died.
W/BinderNative(   85): Uncaught exception from death notification
W/BinderNative(   85): java.lang.IllegalArgumentException: Service not registere
d: com.android.server.TextServicesManagerService$InternalServiceConnection@417c4
b78
W/BinderNative(   85):  at android.app.LoadedApk.forgetServiceDispatcher(LoadedA
pk.java:888)
W/BinderNative(   85):  at android.app.ContextImpl.unbindService(ContextImpl.jav
a:1147)
W/BinderNative(   85):  at com.android.server.TextServicesManagerService$SpellCh
eckerBindGroup.cleanLocked(TextServicesManagerService.java:592)
W/BinderNative(   85):  at com.android.server.TextServicesManagerService$SpellCh
eckerBindGroup.removeListener(TextServicesManagerService.java:575)
W/BinderNative(   85):  at com.android.server.TextServicesManagerService$Interna
lDeathRecipient.binderDied(TextServicesManagerService.java:662)
W/BinderNative(   85):  at android.os.BinderProxy.sendDeathNotice(Binder.java:41
7)
W/BinderNative(   85):  at dalvik.system.NativeStart.run(Native Method)
</code></pre>
