Android 需要使用AsyncTask使应用程序在emulator中运行的帮助吗
我是Android编程新手,尝试制作一个通过servlet从数据库收集数据的应用程序。在我被迫重新安装Eclipse之前,该应用程序一直在工作。现在我似乎在从servlet检索数据时遇到了问题。应用程序符合要求并运行,但未显示“灯光数据”。AsyncTask显然有助于解决这个问题(尽管我确实创建了一个带有简单的enter app按钮的主页,并将其设置为我的新主活动)。我非常感谢您通过使用AsyncTask为我的代码提供特定的答案,谢谢 连接到servlet的代码:通过主页(新建主活动)按钮输入后,应显示此页面:Android 需要使用AsyncTask使应用程序在emulator中运行的帮助吗,android,android-emulator,android-asynctask,Android,Android Emulator,Android Asynctask,我是Android编程新手,尝试制作一个通过servlet从数据库收集数据的应用程序。在我被迫重新安装Eclipse之前,该应用程序一直在工作。现在我似乎在从servlet检索数据时遇到了问题。应用程序符合要求并运行,但未显示“灯光数据”。AsyncTask显然有助于解决这个问题(尽管我确实创建了一个带有简单的enter app按钮的主页,并将其设置为我的新主活动)。我非常感谢您通过使用AsyncTask为我的代码提供特定的答案,谢谢 连接到servlet的代码:通过主页(新建主活动)按钮输入后
package com.example.clearlight;
导入android.os.Bundle;
导入android.app.Activity;
导入android.widget.TextView;
导入android.widget.Toast;
导入java.net.URL;
导入org.apache.http.client.ResponseHandler;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.impl.client.BasicResponseHandler;
导入org.apache.http.impl.client.DefaultHttpClient;
导入android.os.StrictMode;
导入android.util.Log;
公共类MainActivity扩展了活动{
文本视图;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
/*StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略)*/
setContentView(R.layout.relative);
//创建一个粗略的视图-这实际上应该通过布局资源进行设置,但因为它是一个示例,所以可以在XML中声明它们。
/*LinearLayout rootLayout=新的LinearLayout(getApplicationContext());
txt=新文本视图(getApplicationContext());
rootLayout.addView(txt);
setContentView(rootLayout)*/
URL=null;
DefaultHttpClient httpclient=null;
试一试{
字符串注册URL=”http://10.0.2.2/SensorInfo/GetLightData?sensor=light";
url=新url(注册url);
HttpGet getRequest=新的HttpGet(注册URL);
ResponseHandler=new BasicResponseHandler();
httpclient=新的DefaultHttpClient();
//从服务器请求数据
String result=httpclient.execute(getRequest,handler);
Log.d(“MyApp”,“来自服务器的数据是”+结果);
//创建TextView变量**********************************
TextView text1=(TextView)findViewById(R.id.text);
//将新文本设置为TextView(运行时单击事件)//*******
text1.setText(“灯光数据=”+结果);
Toast.makeText(这是“Light Data:+result,Toast.LENGTH_SHORT).show();//消息框
//txtMessage.setText(String.valueOf(msg1)+“”+String.valueOf(msg2));
}捕获(例外情况除外){
例如printStackTrace();
}
}
}
舱单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.clearlight"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.clearlight.MainActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.clearlight.HomePage"
android:label="@string/homepage"
android:parentActivityName="com.example.clearlight.MainActivity" >
<!-- Moved the intent filter to HomePage -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.clearlight.MainActivity" />
</activity>
</application>
</manifest>
对于android,最好不要在活动中运行网络请求。当您尝试建立网络连接时,可能会出现超时,或者可能会加载大量数据,从而使代码在该行“冻结”。活动的人往往讨厌等待,因为此时屏幕没有响应。当这种情况持续很长时间后,android将(可能会强制)关闭你的应用程序 正如您提到的,异步任务是从网络连接加载数据的更好方法。AsyncTasks的工作方式或多或少类似于普通类,但有一些细节除外。链接应该让你了解这些细节
对于您的错误:就像上面Vortex所说的,检查您是否没有陷入错误。在catch语句中添加:Log.e(“error”,e.toString());并检查您的LogCat中是否没有wall-o-red-text。因此通常不建议从UI线程执行任何网络连接。对于Android<3.0,它可以工作,但在Android 3.0中它是固定的且不允许的(NetworkOnMainThreadException被抛出) 所以这是一个很好的选择,非常适合这样的用途。简单地说,AsyncTask的关键方法是在后台线程上运行(也用于像您这样的日志运行操作),并表示执行Internet连接的正确位置 在您与Servlet通信的代码中,您应该将其放入此方法中,结果将以字符串形式返回响应 然后,这个字符串被发送到方法中,该方法在后台操作doInBackground()方法完成时被调用,在这里,您将更新UI(将响应设置为相应小部件的内容)。OnPostExecute()已在UI线程上运行并允许UI更新 如果您有点困惑,最好阅读一些教程:
catch(Exception ex){Log.e(“error”,e.toString());ex.printStackTrace();}
哦,我的错!我总是习惯性地调用我的异常e,但当查看代码时,我看到您将其称为ex。将其更改为Log.e(“error”,ex.toString())代码>确定,所以当我点击我的主页输入按钮时,我会看到一个黑屏(没有响应)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.clearlight"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.clearlight.MainActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.example.clearlight.HomePage"
android:label="@string/homepage"
android:parentActivityName="com.example.clearlight.MainActivity" >
<!-- Moved the intent filter to HomePage -->
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.clearlight.MainActivity" />
</activity>
</application>
</manifest>