2016-10-22 4 views
0

Ich bin ein Anfänger in Android, ich frage mich, wie ich Daten von meinem oncreateView an meine asynchrone Aufgabe übergeben. Zum Beispiel habe ich eine Benutzer-ID in meinem oncreate Wie kann ich diese Benutzer-ID zu meinem asyntask aufrufen?onCreateView-Wert zu AsyncLogin

Hier ist mein 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(); 
     } 

Antwort

1

Sie params zum execute Methode übergeben kann wie folgt,

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

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

ich doInBackground nside abrufen es als,

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

Danke, dass ich das benutzt habe und es funktioniert hat omg Du hast meinen Schlussstein gerettet HAHAHAHA <3 Mehr Power –

1

Auf der onCreateview

new AsyncLogin(yourId).execute(); 

Und einen Konstruktor in der AsyncTask erstellen:

int id; 

public AsyncLogin(int id) { 
    super(); 
    this.id = id; 
} 
+0

Ill um diesem das andere Mal versuchen. Vielen Dank!!!!! –

Verwandte Themen