简体   繁体   中英

Getting string from mainactivity to an activity that extends AsyncTask, using edittext

I need to pass a string to AsyncTask by taking an input from MainActivity to put that string to an url which is present in the AsyncTask. I have two activities, one is MainActivity and another is fetchData activity. My MainActivity code:

public class MainActivity extends AppCompatActivity {
Button click;
EditText text;
public  static TextView data;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    click = (Button) findViewById(R.id.button);
    data = (TextView) findViewById(R.id.fetcheddata);
    text = (EditText) findViewById(R.id.editText);
    String theText = text.getText().toString();
    click.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {fetchData process = new fetchData();process.execute();}});}}

And my fetchData Activity code goes like this:

public class fetchData extends AsyncTask<Void,Void,Void>  {
String data ="";
String dataParsed = "";
String singleParsed ="";
String uriString;
@Overrideprotected Void doInBackground(Void... voids) {
    try {
        URL url = new URL("http://fuelpriceindia.herokuapp.com/price?city=mumbai");
        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;}
        JSONObject JO = new JSONObject(data);
         }} 
       catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }return null;}@Override protected void onPostExecute(Void aVoid){super.onPostExecute(aVoid);MainActivity.data.setText(this.dataParsed);}}`

I want to pass a string to get any city name in the url URL url = new URL("http://fuelpriceindia.herokuapp.com/price?city=mumbai"); city=any city name got from the edit text.

There are multiple ways to pass data to AsyncTask .

  1. create a constructor with parameter in your AysncTask like -

     public void fetchData(String city) { }
  2. Pass value in parameter of AsyncTask like -

     fetchData process = new fetchData(); process.execute(text.getText().toString()); // Pass parameter

In your AsyncTask get this value

@Override
protected Void doInBackground(String...params) 
{
    String cityName = params[0];
    try {
    URL url = new URL("http://fuelpriceindia.herokuapp.com/price?city="+cityName);
      .......

}

Make sure to convert your

 public class fetchData extends AsyncTask<Void,Void,Void> 

into

 public class fetchData extends AsyncTask<String,Void,Void> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM