Good day to all. I'm still completely new, completely confused, I ask for your help. Code:

import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import java.io.IOException; public class MainActivity extends AppCompatActivity { Button mButton; EditText mEditText; TextView mTextView; Parser par; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTextView = (TextView) findViewById(R.id.mTextView); mButton = (Button) findViewById(R.id.mButton); mEditText = (EditText) findViewById(R.id.mEditText); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String code = mEditText.getText().toString(); par = new Parser(code); par.execute(); } }); } } class Parser extends AsyncTask<String, Void, Void> { public Parser(String code) { this.code = code; } String link; String code; String firstlink; String secondlink; TextView mTextView; @Override protected Void doInBackground(String... params) { Document doc = null; link = "http://meteoinfo.by/radar/?q=" + code; try { doc = Jsoup.connect(link).get(); } catch (IOException e) { e.printStackTrace(); } if (doc != null) { firstlink = doc.select("td img[border=\"0\"]").attr("src"); secondlink = "http://meteoinfo.by/radar" + firstlink.replaceFirst("\\.", ""); } else secondlink = "Ошибка"; return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); mTextView.setText(secondlink); } } 

Please mEditText how to send a code from mEditText to the parser class so that it can process it and get a secondlink secondlink at the output and insert it into the mTextView .

xml:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.pollux.test2.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/mTextView" android:text="link must be here" android:textSize="20dp"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="get link" android:id="@+id/mButton" android:layout_below="@+id/mTextView" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"/> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="RUKL" android:id="@+id/mEditText" android:layout_below="@+id/mButton" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"/> </RelativeLayout> 
  • You need to read / watch the AsyncTask . If someone writes this code instead of you, you will not learn anything. Better figure it out yourself. google.com.ua/… - Vladyslav Matviienko
  • @metalurgus lessons are certainly a must-have, but even when you understand the example code, especially your own work, it is even better understood. If it is not difficult for you, please help with this code. - Pollux

1 answer 1

You can pass the necessary data through the constructor:

 class parser extends AsyncTask<String, Void, Void> { String code; public parser(String code) { this.code = code; } ... } 

Call the task, respectively, as follows:

  new parser(code).execute(); 

You need to onPostExecute() UI in the onPostExecute() method - it is in it that you can assign something.


At the same time, at the moment, you can see the lack of understanding of the AsyncTask operation - it is better to do it in a separate class and transfer to it a class that implements the interface (activation, for example), which should be responsible for assigning text to the widget.

Also, classes in Java are capitalized.

  • thanks for the advice. Updated the first post, it seems to be nothing, but for some reason in the onPostExecute() method the string mTextView.setText(secondlink) underlines the red mTextView . Everything seems to be like a lesson , but something is probably wrong - Pollux
  • @Pollux, hover over this place and a hint with the type of error will appear - YuriiSPb
  • Naturally suggestive, the error "cannot resolve symbol mTextView ", recheck the code, walked on Google who had it, everything seems to be in order. Most likely, everything is all right for me in the code) - Pollux
  • @Pollux, in general, it seems, the variable should be visible ... But, if not, then try to pass the constructor as well. This is a bad approach, but it should work. - Yuriy SPb
  • If I correctly understood you, I added this: in the constructor public Parser(String code, String mTextView) added the line this.mTextView = mTextView; , but here it is ( mTextView ) red with the same error. Maybe try, if not difficult, in your editor this code. Above enclosed xml. - Pollux