Created
December 6, 2012 06:24
-
-
Save Gwindow/4222199 to your computer and use it in GitHub Desktop.
android what.cd login example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity" > | |
<TextView | |
android:id="@+id/dispText" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerHorizontal="true" | |
android:layout_centerVertical="true" /> | |
<Button | |
android:id="@+id/button" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_above="@+id/dispText" | |
android:layout_centerHorizontal="true" | |
android:text="Login" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ct.whatcd; | |
import android.app.Activity; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.view.Menu; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import api.soup.MySoup; | |
import api.util.CouldNotLoadException; | |
public class MainActivity extends Activity implements OnClickListener { | |
private static final String SSL_WHAT_CD = "ssl.what.cd"; | |
private LoginTask loginTask = null; | |
private Button button; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
MySoup.setSite(SSL_WHAT_CD); | |
TextView dispText = (TextView) findViewById(R.id.dispText); | |
dispText.setText("Hello!"); | |
// instantiate the button and bind an onclicklistener to it | |
button = (Button) findViewById(R.id.button); | |
button.setOnClickListener(this); | |
} | |
@Override | |
public void onClick(View v) { | |
// make sure the correct button was clicked by checking ids. this doesn't matter with 1 button but as you get | |
// more it will | |
if (v.getId() == button.getId()) { | |
// execute the asynctask | |
loginTask = new LoginTask(); | |
loginTask.execute(); | |
} | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.activity_main, menu); | |
return true; | |
} | |
private class LoginTask extends AsyncTask<Void, Void, Boolean> { | |
@Override | |
protected void onPreExecute() { | |
// do stuff like show the progress dialog | |
} | |
@Override | |
protected Boolean doInBackground(Void... params) { | |
// do the api request here and return true or false depending on the result | |
boolean success = false; | |
try { | |
MySoup.login("login.php", "USERNAME", "PASSWORD"); | |
success = true; | |
} catch (CouldNotLoadException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return success; | |
} | |
@Override | |
protected void onPostExecute(final Boolean success) { | |
// do stuff depending on whether the result was succesful or not, typically I another method on a successful | |
// result to keep code organized | |
if (success) { | |
Toast.makeText(MainActivity.this, "Logged in!", Toast.LENGTH_LONG).show(); | |
} else { | |
Toast.makeText(MainActivity.this, "Login failed :(", Toast.LENGTH_LONG).show(); | |
} | |
} | |
@Override | |
protected void onCancelled() { | |
loginTask = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment