android - 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 连接。我正在解析结果并以字符串显示。
解决方案
拆分new HTTPConnection1.execute();
为:
HTTPConnection1 conn = new HTTPConnection1();
result = conn.execute();
推荐阅读
- c# - Sql server 网格和文本输出不同的行为
- java - 如何基于 Java 中的稀疏列矩阵参数创建二维数组?
- wordpress - 每次我为 WordPress 运行 docker-compose up 时,数据库都是空的
- python - 生成具有不同大小数组的 4D 数组
- python - 是否有正则表达式模式,它返回相同字符的所有完全匹配?
- java - 如何运行 Grid 并运行
- c - 如何在 C 中使用 scanf() 读取一行?
- reactjs - 自动完成:如何用您自己的 react-select 元素替换 Webchat 的输入字段
- vue.js - Vue Press Github Pages .md 文件不起作用
- c# - 如何使应用程序线程安全?