【发布时间】:2014-08-04 15:21:18
【问题描述】:
我想执行一个 AsyncTask 但它不工作
public class httpConn extends Activity{
LoginClass newLogin = new LoginClass();
LoginClass loginRslt = new LoginClass();
TextView data,strConn,strTry,bfrTry;
Button btnFetch;
String[] baseUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
data = (TextView) findViewById(R.id.tvData);
strConn = (TextView) findViewById(R.id.tvConn);
strTry = (TextView) findViewById(R.id.tvTry);
bfrTry = (TextView) findViewById(R.id.tvBfrTry);
btnFetch = (Button) findViewById(R.id.bFetch);
baseUrl[0] = " ";
new ReadDataTask().execute(baseUrl);
btnFetch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
bfrTry.setText("before_try Success");
strTry.setText("try Success");
}
});
}
private abstract class ReadDataTask extends AsyncTask <String, integer, LoginClass>{
@Override
protected LoginClass doInBackground(String...params) {
// TODO Auto-generated method stub
XmlPullParserFactory factory;
try{
URL url = new URL(params[0]);
URLConnection conn = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) conn;
int responseCode = httpConn.getResponseCode();
if(responseCode == HttpURLConnection.HTTP_OK){
strConn.setText("Success");
InputStream in = httpConn.getInputStream();
factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(in, null);
int eventType = xpp.getEventType();
while(!(eventType == XmlPullParser.END_DOCUMENT))
{
if(eventType == XmlPullParser.START_TAG &&
xpp.getName().equals("LoginModel")){
eventType = xpp.next();
while(!(eventType == XmlPullParser.END_TAG &&
xpp.getName().equals("LoginModel"))){
if(eventType == XmlPullParser.START_TAG &&
xpp.getName().equals("_ErrorFound")){
newLogin.set_ErrorFound(xpp.nextText());
eventType = xpp.next();
}else if (eventType == XmlPullParser.START_TAG &&
xpp.getName().equals("_UserID")){
newLogin.set_UserID(xpp.nextText());
eventType = xpp.next();
}else if (eventType == XmlPullParser.START_TAG &&
xpp.getName().equals("_FullName")){
newLogin.set_FullName(xpp.nextText());
eventType = xpp.next();
}else if (eventType == XmlPullParser.START_TAG &&
xpp.getName().equals("_ProfilePic")){
newLogin.set_ProfilePic(xpp.nextText());
eventType = xpp.next();
}else if (eventType == XmlPullParser.START_TAG &&
xpp.getName().equals("_ProfilePicExtension")){
newLogin.set_ProfilePicExtension(xpp.nextText());
eventType = xpp.next();
}
}
}
}
return newLogin;
}else{
return null;
}
}catch(XmlPullParserException e){
Log.d("PULLPARSER","XML Pull Parser Exception",e);
return null;
}catch(IOException ioe)
{
Log.d("PULLPARSER","IO Exception",ioe);
return null;
}
}
@Override
protected void onProgressUpdate(integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(LoginClass result) {
// TODO Auto-generated method stub
data.setText(result._FullName +" "+String.valueOf(result._UserID));
super.onPostExecute(result);
}
}
}
错误在一行
new ReadDataTask().execute(baseUrl);
据说
无法实例化类型 httpConn.ReadDataTask
我想要的只是从 web 服务获取数据并解析它并在我的屏幕上显示它
这是 LogCat
08-04 18:28:11.425: E/AndroidRuntime(22284): FATAL EXCEPTION: AsyncTask #1
08-04 18:28:11.425: E/AndroidRuntime(22284): Process: com.example.httpmob, PID: 22284
08-04 18:28:11.425: E/AndroidRuntime(22284): java.lang.RuntimeException: An error occured while executing doInBackground()
08-04 18:28:11.425: E/AndroidRuntime(22284): at android.os.AsyncTask$3.done(AsyncTask.java:300)
08-04 18:28:11.425: E/AndroidRuntime(22284): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
08-04 18:28:11.425: E/AndroidRuntime(22284): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
08-04 18:28:11.425: E/AndroidRuntime(22284): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
08-04 18:28:11.425: E/AndroidRuntime(22284): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-04 18:28:11.425: E/AndroidRuntime(22284): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-04 18:28:11.425: E/AndroidRuntime(22284): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-04 18:28:11.425: E/AndroidRuntime(22284): at java.lang.Thread.run(Thread.java:811)
08-04 18:28:11.425: E/AndroidRuntime(22284): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
08-04 18:28:11.425: E/AndroidRuntime(22284): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6024)
08-04 18:28:11.425: E/AndroidRuntime(22284): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:820)
08-04 18:28:11.425: E/AndroidRuntime(22284): at android.view.View.requestLayout(View.java:16431)
08-04 18:28:11.425: E/AndroidRuntime(22284): at android.view.View.requestLayout(View.java:16431)
08-04 18:28:11.425: E/AndroidRuntime(22284): at android.view.View.requestLayout(View.java:16431)
08-04 18:28:11.425: E/AndroidRuntime(22284): at android.view.View.requestLayout(View.java:16431)
08-04 18:28:11.425: E/AndroidRuntime(22284): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:352)
08-04 18:28:11.425: E/AndroidRuntime(22284): at android.view.View.requestLayout(View.java:16431)
08-04 18:28:11.425: E/AndroidRuntime(22284): at android.view.View.requestLayout(View.java:16431)
08-04 18:28:11.425: E/AndroidRuntime(22284): at android.widget.TextView.checkForRelayout(TextView.java:6600)
08-04 18:28:11.425: E/AndroidRuntime(22284): at android.widget.TextView.setText(TextView.java:3813)
08-04 18:28:11.425: E/AndroidRuntime(22284): at android.widget.TextView.setText(TextView.java:3671)
08-04 18:28:11.425: E/AndroidRuntime(22284): at android.widget.TextView.setText(TextView.java:3646)
08-04 18:28:11.425: E/AndroidRuntime(22284): at com.example.httpmob.httpConn$ReadDataTask.doInBackground(httpConn.java:77)
08-04 18:28:11.425: E/AndroidRuntime(22284): at com.example.httpmob.httpConn$ReadDataTask.doInBackground(httpConn.java:1)
08-04 18:28:11.425: E/AndroidRuntime(22284): at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-04 18:28:11.425: E/AndroidRuntime(22284): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-04 18:28:11.425: E/AndroidRuntime(22284): ... 4 more
【问题讨论】:
-
它是抽象的,就是抽象的作用。
标签: java android android-asynctask