我怎样才能从网页中获取一个值并在 main class 上使用它? Android 应用
How can i get a value from webpage and use it on main class ? Android app
我正在尝试读取网页上的内容并将其存储在名为 "finalresult" 的 var 中。我阅读了文本,我使用了 HttpURLConnection,我在 AsyncTask 的 doInBacgorund 中完成了它。
我会告诉你我的代码:
public class MainActivity extends AppCompatActivity {
public String finalresult = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
class MyRemote extends AsyncTask<Void, Void, String> {
URL url;
HttpURLConnection connection = null;
@Override
protected String doInBackground(Void... params) {
try
{
//Create connection
url = new URL("My url bla bla");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.flush();
wr.close();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
finalresult = response.toString();
} catch (Exception e) {
e.printStackTrace();
} finally
{
if (connection != null) {
connection.disconnect();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
当我想在 Main Activity Class 中使用 "finalresult" var 时,我不能,因为它是空的。我怎样才能在我的主要 ACTIVITY CLASS 中获得该结果?
谢谢。
顺便说一句,我是初学者。
请查看 Android 的 AsyncTask 文档。另外,我不确定您是否缺少括号或其他内容,但请注意您的 MyRemote
class 声明不能在 onCreate()
方法内。
您的 finalResult
变量为空的原因是因为您从未真正使用过您实现的 MyRemote
class。
所以你需要
new MyRemote().execute();
在您的 onCreate()
方法中。另外,请记住,因为此请求是 异步的 ,所以在 onPostExecute()
方法中使用 finalResult
变量是有意义的。
此外,像
那样对 URL 进行硬编码并不是一个好主意
url = new URL("My url bla bla");
相反,它应该作为参数传递给 execute()
方法。再一次,看看文档,它应该会变得更清楚。
我正在尝试读取网页上的内容并将其存储在名为 "finalresult" 的 var 中。我阅读了文本,我使用了 HttpURLConnection,我在 AsyncTask 的 doInBacgorund 中完成了它。
我会告诉你我的代码:
public class MainActivity extends AppCompatActivity {
public String finalresult = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
class MyRemote extends AsyncTask<Void, Void, String> {
URL url;
HttpURLConnection connection = null;
@Override
protected String doInBackground(Void... params) {
try
{
//Create connection
url = new URL("My url bla bla");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.flush();
wr.close();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
finalresult = response.toString();
} catch (Exception e) {
e.printStackTrace();
} finally
{
if (connection != null) {
connection.disconnect();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}
}
当我想在 Main Activity Class 中使用 "finalresult" var 时,我不能,因为它是空的。我怎样才能在我的主要 ACTIVITY CLASS 中获得该结果?
谢谢。 顺便说一句,我是初学者。
请查看 Android 的 AsyncTask 文档。另外,我不确定您是否缺少括号或其他内容,但请注意您的 MyRemote
class 声明不能在 onCreate()
方法内。
您的 finalResult
变量为空的原因是因为您从未真正使用过您实现的 MyRemote
class。
所以你需要
new MyRemote().execute();
在您的 onCreate()
方法中。另外,请记住,因为此请求是 异步的 ,所以在 onPostExecute()
方法中使用 finalResult
变量是有意义的。
此外,像
那样对 URL 进行硬编码并不是一个好主意url = new URL("My url bla bla");
相反,它应该作为参数传递给 execute()
方法。再一次,看看文档,它应该会变得更清楚。