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.