earl cabanig earl cabanig - 1 month ago 7
Android Question

onCreateView value to AsyncLogin

Im a beginner in android, Im wondering as to how to pass data from my oncreateView to my async task. For example I have a user id in my oncreate how can I call this user Id to my asyntask?

here is my code.

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// myView = inflater.inflate(R.layout.currently_history_rvmainfragment,container, false);
myView = inflater.inflate(R.layout.currently_history_rvmainfragment,container, false);
Bundle bundle = this.getArguments();
int myInt = bundle.getString("key","")

//I HAVE A DATA HERE AND I WANT TO PASS IT TO MY ASYNC TASK

new AsyncLogin().execute();
return myView;
}

private class AsyncLogin extends AsyncTask<String, String, String> {
ProgressDialog pdLoading = new ProgressDialog(getActivity());
HttpURLConnection conn;
URL url = null;
MaincommentShowmore maincommentShowmore= new MaincommentShowmore();

// I WANT TO PASS IT HERED SO THAT I MAY CALL IT TO MY URL

@Override
protected void onPreExecute() {
super.onPreExecute();
//this method will be running on UI thread
pdLoading.setMessage("\tLoading...");
pdLoading.setCancelable(false);
pdLoading.show();
}

@Override //SO THAT I MAY ADD INT VALUES IN THIS URL
protected String doInBackground(String... params) {
try {
// Enter URL address where your json file resides
// Even you can make call to php file which returns json data
url = new URL("http://192.168.1.6/jmiappphp/showRateCommentShowMore.php");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return e.toString();
}
try {

// Setup HttpURLConnection class to send and receive data from php and mysql
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(READ_TIMEOUT);
conn.setConnectTimeout(CONNECTION_TIMEOUT);
conn.setRequestMethod("GET");

// setDoOutput to true as we recieve data from json file
conn.setDoOutput(true);

} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return e1.toString();
}

Answer

You can pass params to the execute method as follows,

String data1 = "your_data1";
String data2 = "your_data2";

new AsyncLogin().execute(data1, data2);

Inside doInBackground retrieve it as,

@Override
protected String doInBackground(String... params) {
    String data1 = params[0];
    String data2 = params[1];
}
Comments