package com.humbughq.android;

import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.widget.TextView;

class AsyncLogin extends HumbugAsyncPushTask {

    public AsyncLogin(HumbugActivity humbugActivity, String username,
            String password) {
        super(humbugActivity);
        context = humbugActivity;
        this.context.email = username;
        this.setProperty("username", username);
        this.setProperty("password", password);
    }

    public final void execute() {
        execute("api/v1/fetch_api_key");
    }

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

        if (result != null) {
            this.context.api_key = result.toString();
            Log.i("login", "Logged in as " + this.context.api_key);

            Editor ed = this.context.settings.edit();
            ed.putString("email", this.context.email);
            ed.putString("api_key", this.context.api_key);

            ed.commit();

            this.context.openLogin();
        } else {
            TextView errorText = (TextView) this.context
                    .findViewById(R.id.error_text);
            errorText.setText("Login failed");
        }
    }
}
@@ -24,16 +24,76 @@
 droid;%0A%0A
+import org.json.JSONException;%0Aimport org.json.JSONObject;%0A%0A
 import a
@@ -778,49 +778,213 @@
    t
-his.context.api_key = result.toString();%0A
+ry %7B%0A                JSONObject obj = new JSONObject(result);%0A%0A                if (obj.getString(%22result%22).equals(%22success%22)) %7B%0A                    this.context.api_key = obj.getString(%22api_key%22);%0A        
     
@@ -1060,16 +1060,24 @@
         
+     
+   
 Editor e
@@ -1106,24 +1106,32 @@
 ngs.edit();%0A
+        
             
@@ -1177,32 +1177,40 @@
 l);%0A            
+  
+      
 ed.putString(%22ap
@@ -1253,16 +1253,24 @@
         
+       
+ 
 ed.commi
@@ -1271,24 +1271,32 @@
 .commit();%0A%0A
+        
             
@@ -1333,21 +1333,198 @@
     
-%7D else %7B%0A    
+            return;%0A                %7D%0A            %7D catch (JSONException e) %7B%0A                // TODO Auto-generated catch block%0A                e.printStackTrace();%0A            %7D%0A        %7D%0A
     
@@ -1588,20 +1588,16 @@
         
-    
 .findVie
@@ -1628,20 +1628,16 @@
         
- 
-   
 errorTex
@@ -1663,25 +1663,15 @@
 iled%22);%0A
-        %7D%0A
     %7D%0A%7D

Refactor login code to handle JSON return values for api_key method.