
不起作用的简单概念:
我正在尝试连接到网络并使用异步连接检索一些数据.当连接返回数据时,我想切换到另一个活动.我的代码无法正常工作.
我假设我需要使用某种回调,但是我是Android / Java的新手,因此无法通过Googling找出如何做到这一点.有人可以看看并建议我如何创建一个在数据返回时启动意图的回调吗?:
*更新:我在这里找到了一个不错的库http://loopj.com/android-async-http/,这是在后台连接到网络的另一种(简便)方法.
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); }}解决方法:
使用AsyncTask进行作业,并在AsyncTasks onPostExecute方法调用中触发新的Activity.
总结以上是内存溢出为你收集整理的异步连接返回时如何切换活动?全部内容,希望文章能够帮你解决异步连接返回时如何切换活动?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)