Search code examples
javaandroideclipsesyntax-errornested-class

Syntax error on token "class"


I am trying to create a nested class in order to use the AsyncTask, however eclipse gives me an error on the class SendData saying "Syntax error on token "class", invalid type" why is it giving me that error?

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

}

Solution

  • sendData() is a method syntax, not a class

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

    should be

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