Java 请在我的AsyncTask课程中帮助我

Java 请在我的AsyncTask课程中帮助我,java,android,android-asynctask,Java,Android,Android Asynctask,这是我的java文件。似乎找不到如何执行post请求方法的答案 请教我如何做这件事。谢谢在使用onClickListener时,我需要在cgi脚本上发布请求 package com.example.miraapp; import java.io.IOException; import java.net.URI; import org.apache.http.HttpResponse; import org.apache.http.client.Cli

这是我的java文件。似乎找不到如何执行post请求方法的答案 请教我如何做这件事。谢谢在使用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;


        }
    }
};

也许更像这样:

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;


        }
    }
}
package com.example.miraapp;
导入java.io.IOException;
导入java.net.URI;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入android.app.Activity;
导入android.app.DownloadManager;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.ImageButton;
导入android.widget.Toast;
公共类GUI扩展活动实现OnClickListener{
图像按钮IB1;
图像按钮IB2;
图像按钮IB3;
图像按钮IB4;
公共URI[]URL;
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u gui);
IB1=(ImageButton)findViewById(R.id.imageButton1);
IB1.setOnClickListener(此);
IB2=(ImageButton)findViewById(R.id.imageButton2);
IB2.setOnClickListener(此);
IB3=(ImageButton)findViewById(R.id.imageButton3);
IB3.setOnClickListener(本);
IB4=(ImageButton)findViewById(R.id.imageButton4);
IB4.setOnClickListener(此);
}
类RequestTask扩展了AsyncTask{
异常错误;
@凌驾
受保护的Void doInBackground(字符串…参数){
字符串url=params[0];
//创建一个新的HttpClient和Post头
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
试一试{
//执行HTTP Post请求
httpclient.execute(httppost);
}捕获(客户端协议例外e){
误差=e;
}捕获(IOE异常){
误差=e;
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
if(错误!=null){
//TODO在此处处理异常,例如:
Toast.makeText(GUI.this,“Error:+Error.getMessage(),Toast.LENGTH\u SHORT.show();
}
}
}
公共无效ibutton4Click()
{
新建RequestTask().execute(“10.0.0.1/cgi-bin/ForwardPress.cgi”);
Toast.makeText(GUI.this,“Forward!”,Toast.LENGTH\u SHORT.show();
}
公共无效IButtonerease()
{
newrequestTask().execute(“10.0.0.1/cgi-bin/ButtonRelease.cgi”);
}
公共无效ibutton2Click()
{
newrequesttask().execute(“10.0.0.1/cgi-bin/BackwardPress.cgi”);
Toast.makeText(GUI.this,“Backward!”,Toast.LENGTH\u SHORT.show();
}
公共无效ibutton3Click()
{
新建RequestTask().execute(“10.0.0.1/cgi-bin/RightPress.cgi”);
Toast.makeText(GUI.this,“右转!”,Toast.LENGTH\u SHORT.show();
}
公共无效ibutton1Click()
{
新建RequestTask().execute(“10.0.0.1/cgi-bin/LeftPress.cgi”);
Toast.makeText(GUI.this,“左转!”,Toast.LENGTH_SHORT.show();
}
公共void onClick(视图v)
{
开关(v.getId())
{
案例R.id.imageButton1:
ibutton1Click();
打破
案例R.id.imageButton2:
ibutton2Click();
打破
案例R.id.imageButton3:
ibutton3Click();
打破
案例R.id.imageButton4:
ibutton4Click();
打破
}
}
}

我希望你能花点时间理解我做了什么以及为什么。此外,您的错误消息表明您有很多语法错误。最好边走边修复,这样你就不会剩下200行代码,也不会不知道哪里出了问题或错误在哪里。

为什么不将postData函数的内容直接放在doInBackGround中?你在添加。在Asyncktask中执行调用?!!谢谢你接受我的回答,现在我有评论权限了!
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;


        }
    }
}