我是一个初学者,我一直在关注一些教程以解析JSON。我即将进行编译并尝试首次运行,但是当我尝试创建Java类的对象并在触发按钮“ click”时使进程运行时,出现错误,提示“无法解析符号” fetchData“”
package com.example.h.arbitrage;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button click;
public static TextView data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
click = findViewById(R.id.button);
data = findViewById(R.id.fetcheddata);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fetchData process = new fetchData();
((fetchData) process).execute();
}
});
}
}
我还包括了fetchData.java的代码,以防万一其中有什么原因。
我一直在寻找答案,这也许很明显,但是我什至没有专门的术语来对此进行适当的研究...
import android.net.UrlQuerySanitizer;
import android.os.AsyncTask;
import com.example.h.arbitrage.MainActivity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class fetchData extends AsyncTask<Void,Void,Void> {
String data = "";
@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("https://");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line ="";
while(line != null){
line = bufferedReader.readLine();
data = data + line;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
MainActivity.data.setText(this.data);
}
}
在此先感谢您的帮助。