,android,http,asynchronous,post"/>

首页 > 解决方案 > new HTTPConnection1.execute() 中的编译器错误;其中 HTTPConnection1 类扩展了 AsyncTask

问题描述

代码:

public class ScanActivity extends AppCompatActivity {
    ImageButton imgButton_bus;
    ImageButton imgButton_hotel;
    EditText HTTPResult;
    String result;
    String paramBusNumber = "abc";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan);
        imgButton_bus = (ImageButton)findViewById(R.id.imageButton_bus);
        HTTPResult = (EditText)findViewById(R.id.HTTP_response);

        imgButton_bus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new HTTPConnection1.execute();

            }
        });
    }
    class HTTPConnection1  extends AsyncTask<String, Void, String> {
        String result;
        @Override
        protected String doInBackground(String... params) {

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://http://tayyab001.base.pk/kami.php");
            try {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("id", "12345"));
                nameValuePairs.add(new BasicNameValuePair("message", "msg"));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse httpResponse = httpclient.execute(httppost);
                InputStream inputStream = httpResponse.getEntity().getContent();
                //HTTPResult.setText("result3");
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                //HTTPResult.setText("result4");
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                //HTTPResult.setText("result5");
                StringBuilder stringBuilder = new StringBuilder();
                //HTTPResult.setText("result6");
                String bufferedStrChunk = null;

                while((bufferedStrChunk = bufferedReader.readLine()) != null){
                    stringBuilder.append(bufferedStrChunk);
                }


                result = stringBuilder.toString();
                //HTTPResult.setText(result);
                //msgTextField.setText(""); // clear text box
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }
            return null;
        }


    }

}

我已经从 Async 扩展了 HTTPConnection1 类。HTTPConnection 类放在里面public class ScanActivity extends AppCompatActivity。我HTTPConnection1.execute();从内部按钮处理程序调用 new 。但是在 new 上出现编译器错误HTTPConnection1.execute();-error: cannot find symbol new HTTPConnection1.execute();在 HTTPConnection 类中我正在创建 HTTP 连接。我正在解析结果并以字符串显示。

标签: androidhttpasynchronouspost

解决方案


拆分new HTTPConnection1.execute();为:

HTTPConnection1 conn = new HTTPConnection1();
                result = conn.execute();

推荐阅读