简体   繁体   English

请在我的AsyncTask类中帮助我

[英]please help me in my AsyncTask class

here is my java file. 这是我的java文件。 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. 使用onClickListener时,我需要在cgi脚本上发布请求。

    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

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. 最好随时进行修复,这样您就不会剩下200行代码,并且您也不知道哪里出了问题或错误在哪里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM