令牌“class”上的语法错误

时间:2012-12-26 21:33:44

标签: java android eclipse syntax-error nested-class

我正在尝试创建一个嵌套类以便使用AsyncTask,但是eclipse在类SendData上给出了一个错误,上面写着“令牌上的语法错误”类,“无效类型”为什么它会给我这个错误?< / p>

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);
    }

}

2 个答案:

答案 0 :(得分:15)

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

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

应该是

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

答案 1 :(得分:3)

private class SendData()

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

如果要定义课程,请删除()