【问题标题】:How can I solve - can not resolve symbol execute我该如何解决 - 无法解析符号执行
【发布时间】:2020-02-29 10:38:38
【问题描述】:

I had seen this error in this line: new JSONTask.execute("https://jsonparsingdemo-cec5b.firebaseapp.com/jsonData/moviesDemoItem.txt");

我正在开发一个创建 URL 连接、JSON 解析、Asintask 的应用程序。但我得到一个错误,我得到了一张图片。

鉴于此错误无法解析符号执行。

公共类 MainActivity 扩展 AppCompatActivity { 私有 TextView tvData;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btnHit = findViewById(R.id.btnHit);
     tvData = findViewById(R.id.tvJsonItem);

    btnHit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new JSONTask.execute("https://jsonparsingdemo-cec5b.firebaseapp.com/jsonData/moviesDemoItem.txt");
        }
    });


}

public class JSONTask extends AsyncTask<String , String , String>{

    @Override
    protected String doInBackground(String... urls) {
        HttpURLConnection connection = null;
        BufferedReader reader = null;

        try {
            URL url = new URL(urls[0]);
            connection =(HttpURLConnection) url.openConnection();
            connection.connect();

            InputStream stream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(stream));
            StringBuffer  buffer = new StringBuffer();
            String line ="";
            while ((line = reader.readLine()) != null){
                buffer.append(line);
            }
            return buffer.toString();


        }catch (MalformedURLException e){
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (connection != null) {
                connection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        tvData.setText(result);
    }
  }
}

我想得到答案

【问题讨论】:

    标签: android json android-asynctask httpurlconnection


    【解决方案1】:

    使用这个

    new JSONTask().execute("https://jsonparsingdemo-cec5b.firebaseapp.com/jsonData/moviesDemoItem.txt");
    

    不是这个

    new JSONTask.execute("https://jsonparsingdemo-cec5b.firebaseapp.com/jsonData/moviesDemoItem.txt");
    

    【讨论】:

    • @NasirUddin 不工作意味着你能解释更多吗?
    • @NasirUddin 很高兴能帮上忙!如果您觉得它对您有用,请随时 accept my answer。 :-)
    猜你喜欢
    • 1970-01-01
    • 2018-04-25
    • 2019-05-10
    • 1970-01-01
    • 2020-04-23
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    相关资源
    最近更新 更多