0

here is my java file. cant seem to find the answer on how to execute a post request method Please teach me on how to make this work. Thanks. i need to post request on a cgi script while using onClickListener.

    package com.example.miraapp;


    import java.io.IOException;
    import java.net.URI;

    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;

    import android.app.Activity;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ImageButton;
    import android.widget.Toast;

    public class GUI extends Activity implements OnClickListener{

    ImageButton IB1;
    ImageButton IB2;
    ImageButton IB3;
    ImageButton IB4;
    public URI[] urls;

    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gui);

        IB1 = (ImageButton) findViewById(R.id.imageButton1);
        IB1.setOnClickListener(this);

        IB2 = (ImageButton) findViewById(R.id.imageButton2);
        IB2.setOnClickListener(this);

        IB3 = (ImageButton) findViewById(R.id.imageButton3);
        IB3.setOnClickListener(this);

        IB4 = (ImageButton) findViewById(R.id.imageButton4);
        IB4.setOnClickListener(this);


    }
    class RequestTask extends AsyncTask<String, String, String>{

        @Override
        protected String doInBackground(String... params) {


            postData();
            return null;
        }


            public void postData() {
                // Create a new HttpClient and Post Header
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("10.0.0.1/cgi-bin/ForwardPress.cgi");

                try {

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);

                } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                } catch (IOException e) {
                // TODO Auto-generated catch block
                }

                public void ibutton4Click()
                {
                    try {

                        // Execute HTTP Post Request
                        HttpResponse response = httpclient.execute(httppost);

                        } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                        } catch (IOException e) {
                        // TODO Auto-generated catch block
                        }
                    Toast.makeText(this, "Forward!", Toast.LENGTH_SHORT).show();
                }
                public void ibuttonRelease()
                {
                    new RequestTask().execute("10.0.0.1/cgi-bin/ButtonRelease.cgi");
                }
                public void ibutton2Click()
                {
                    new RequestTask().execute("10.0.0.1/cgi-bin/BackwardPress.cgi");
                    Toast.makeText(this, "Backward!", Toast.LENGTH_SHORT).show();
                }
                public void ibutton3Click()
                {
                    new RequestTask().execute("10.0.0.1/cgi-bin/RightPress.cgi");
                    Toast.makeText(this, "Turn Right!", Toast.LENGTH_SHORT).show();
                }
                public void ibutton1Click()
                {
                    new RequestTask().execute("10.0.0.1/cgi-bin/LeftPress.cgi");
                    Toast.makeText(this, "Turn Left!", Toast.LENGTH_SHORT).show();
                }

    }
    }





    public void onClick(View v)
    {

        switch (v.getId())
        {
            case R.id.imageButton1:

                ibutton1Click();
                break;

            case R.id.imageButton2:

                ibutton2Click();
                break;

            case R.id.imageButton3:

                ibutton3Click();
                break;

            case R.id.imageButton4:

                ibutton4Click();
                break;


        }
    }
};

here is my errors

Description Resource    Path    Location    Type
Syntax error, insert "enum Identifier" to complete EnumHeaderName   GUI.java    /MiRaApp/src/com/example/miraapp    line 75 Java Problem
Syntax error on token "void", @ expected    GUI.java    /MiRaApp/src/com/example/miraapp    line 75 Java Problem
Syntax error on token "void", @ expected    GUI.java    /MiRaApp/src/com/example/miraapp    line 89 Java Problem
Syntax error, insert "EnumBody" to complete BlockStatement  GUI.java    /MiRaApp/src/com/example/miraapp    line 75 Java Problem
Syntax error, insert "EnumBody" to complete BlockStatement  GUI.java    /MiRaApp/src/com/example/miraapp    line 89 Java Problem
Syntax error, insert "enum Identifier" to complete EnumHeaderName   GUI.java    /MiRaApp/src/com/example/miraapp    line 89 Java Problem
Syntax error, insert "enum Identifier" to complete EnumHeaderName   GUI.java    /MiRaApp/src/com/example/miraapp    line 93 Java Problem
Syntax error on token "void", @ expected    GUI.java    /MiRaApp/src/com/example/miraapp    line 93 Java Problem
Syntax error on token "void", @ expected    GUI.java    /MiRaApp/src/com/example/miraapp    line 98 Java Problem
Syntax error, insert "EnumBody" to complete BlockStatement  GUI.java    /MiRaApp/src/com/example/miraapp    line 93 Java Problem
Syntax error, insert "EnumBody" to complete BlockStatement  GUI.java    /MiRaApp/src/com/example/miraapp    line 98 Java Problem
Syntax error, insert "enum Identifier" to complete EnumHeaderName   GUI.java    /MiRaApp/src/com/example/miraapp    line 98 Java Problem
Syntax error on token "void", @ expected    GUI.java    /MiRaApp/src/com/example/miraapp    line 103    Java Problem
Syntax error, insert "enum Identifier" to complete EnumHeaderName   GUI.java    /MiRaApp/src/com/example/miraapp    line 103    Java Problem
Syntax error, insert "EnumBody" to complete BlockStatement  GUI.java    /MiRaApp/src/com/example/miraapp    line 103    Java Problem
The method ibutton1Click() is undefined for the type GUI    GUI.java    /MiRaApp/src/com/example/miraapp    line 123    Java Problem
The method ibutton2Click() is undefined for the type GUI    GUI.java    /MiRaApp/src/com/example/miraapp    line 128    Java Problem
The method ibutton3Click() is undefined for the type GUI    GUI.java    /MiRaApp/src/com/example/miraapp    line 133    Java Problem
The method ibutton4Click() is undefined for the type GUI    GUI.java    /MiRaApp/src/com/example/miraapp    line 138    Java Problem
2
  • why you don't put the content of postData function directly inside doInBackGround ?
    – user1650415
    Apr 13, 2014 at 15:08
  • you are adding .execute call inside Asyncktask ?!!
    – user1650415
    Apr 13, 2014 at 15:11

1 Answer 1

0

Perhaps something more like this:

package com.example.miraapp;


import java.io.IOException;
import java.net.URI;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.app.DownloadManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

public class GUI extends Activity implements OnClickListener{

    ImageButton IB1;
    ImageButton IB2;
    ImageButton IB3;
    ImageButton IB4;
    public URI[] urls;

    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gui);

        IB1 = (ImageButton) findViewById(R.id.imageButton1);
        IB1.setOnClickListener(this);

        IB2 = (ImageButton) findViewById(R.id.imageButton2);
        IB2.setOnClickListener(this);

        IB3 = (ImageButton) findViewById(R.id.imageButton3);
        IB3.setOnClickListener(this);

        IB4 = (ImageButton) findViewById(R.id.imageButton4);
        IB4.setOnClickListener(this);


    }
    class RequestTask extends AsyncTask<String, Void, Void>{

        Exception error;
        @Override
        protected Void doInBackground(String... params) {


            String url = params[0];
            // Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);

            try {

                // Execute HTTP Post Request
                httpclient.execute(httppost);

            } catch (ClientProtocolException e) {
                error = e;
            } catch (IOException e) {
                error = e;
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result){
            if(error != null){
                // TODO handle the exception here, for example:
                Toast.makeText(GUI.this, "Error: " + error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }
    }
    public void ibutton4Click()
    {
        new RequestTask().execute("10.0.0.1/cgi-bin/ForwardPress.cgi");
        Toast.makeText(GUI.this, "Forward!", Toast.LENGTH_SHORT).show();
    }
    public void ibuttonRelease()
    {
        new RequestTask().execute("10.0.0.1/cgi-bin/ButtonRelease.cgi");
    }
    public void ibutton2Click()
    {
        new RequestTask().execute("10.0.0.1/cgi-bin/BackwardPress.cgi");
        Toast.makeText(GUI.this, "Backward!", Toast.LENGTH_SHORT).show();
    }
    public void ibutton3Click()
    {
        new RequestTask().execute("10.0.0.1/cgi-bin/RightPress.cgi");
        Toast.makeText(GUI.this, "Turn Right!", Toast.LENGTH_SHORT).show();
    }
    public void ibutton1Click()
    {
        new RequestTask().execute("10.0.0.1/cgi-bin/LeftPress.cgi");
        Toast.makeText(GUI.this, "Turn Left!", Toast.LENGTH_SHORT).show();
    }



    public void onClick(View v)
    {

        switch (v.getId())
        {
            case R.id.imageButton1:

                ibutton1Click();
                break;

            case R.id.imageButton2:

                ibutton2Click();
                break;

            case R.id.imageButton3:

                ibutton3Click();
                break;

            case R.id.imageButton4:

                ibutton4Click();
                break;


        }
    }
}

I hope you'll take the time to understand what I did and why. Also, your error messages indicated that you had lots of syntax errors. It's best to fix those as you go, so you're not left with 200 lines of code and you don't understand what's wrong or where the errors are.

1
  • Thanks for accepting my answer, now I have comment privileges!
    – nasch
    Apr 14, 2014 at 1:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.