Java – How do I switch activities when an asynchronous connection returns?

How do I switch activities when an asynchronous connection returns?… here is a solution to the problem.

How do I switch activities when an asynchronous connection returns?

Simple concept of invalidity:

I’m trying to connect to the web and retrieve some data using an asynchronous connection. When the connection returns data, I want to switch to another activity. My code is not working.

I’m assuming I need to use some kind of callback, but I’m new to Android/Java and can’t find an answer on how to do this by googling. Can someone take a look and suggest how I can create a callback that starts an intent when the data returns? :

*Update: I found this nice library herehttp://loopj.com/android-async-http/ This is another (simple) way to connect to the network in the background.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mtmobtest"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.INTERNET" />

<application
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=". MTMobTestActivity" >            
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

<activity
            android:label="MainMenu"
            android:name=". MainMenu" >            
            <intent-filter >
                <action android:name="android.intent.action.MAINMENU" />
                <category android:name="android.intent.category.MAINMENU" />
            </intent-filter>
        </activity>

</application>

</manifest>

MTMobTestActivity.java

package com.mtmobtest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.Toast;
import android.widget.ViewFlipper;
public class MTMobTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

postData("Sup yall");
        setContentView(R.layout.main);
    }

public void postData(String toPost) {
         Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.example.com/test.php");
        This is the data to send
        String myName = "anybody there?"; any data to send
        try {
         Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("action", myName));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
         Execute HTTP Post Request
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String response = httpclient.execute(httppost, responseHandler);
        This is the response from a php application
        String reverseString = response;

ViewFlipper vf = (ViewFlipper) findViewById(R.layout.menu);
         Set an animation from res/anim: I pick push left in
        vf.setAnimation(AnimationUtils.loadAnimation(view.getContext(), R.anim.push_left_in));
        vf.showNext();

Toast.makeText(this, "response" + reverseString, Toast.LENGTH_LONG).show();

} catch (ClientProtocolException e) {
        Toast.makeText(this, "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
         TODO Auto-generated catch block

} catch (IOException e) {
        Toast.makeText(this, "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
         TODO Auto-generated catch block
        }

Intent intent = new Intent(this, MainMenu.class);
        startActivity(intent);

}
}

Solution

Use AsyncTask and use AsyncTasks in onPostExecute A new activity is triggered in the method call.

Related Problems and Solutions