9

我正在尝试创建一个嵌套类以使用 AsyncTask,但是 eclipse 在类 SendData 上给我一个错误,说“令牌“类”上的语法错误,无效类型”为什么它给我那个错误?

package com.example.myfirstapp;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.Bundle;
import android.app.Activity;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.content.Intent;
import android.widget.TextView;

public class DisplayMessageActivity extends Activity {
    String url = "http://www.mySIte.com/phpFIle";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        setContentView(textView);

        new SendData().execute();
    }

    private class SendData() extends AsyncTask<Void, Void, Void>{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

        try{
            List<NameValuePair> nameValues = new ArrayList<NameValuePair>(1);
            nameValues.add(new BasicNameValuePair("id", "12345"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValues));

            System.out.println("Before response");
            HttpResponse response = httpclient.execute(httppost);
            System.out.println("After response");

        }catch(ClientProtocolException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }   
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
4

2 回答 2

17

sendData() 是一种方法语法,而不是一个类

private class SendData() extends AsyncTask<Void, Void, Void>{

应该

private class SendData extends AsyncTask<Void, Void, Void>{
于 2012-12-26T21:34:54.660 回答
4
private class SendData()

你有两个methodclass语法。不确定你想要哪一个。

如果你想定义一个类然后删除()

于 2012-12-26T21:34:49.187 回答