2017-03-28 38 views
0

我目前從遠程數據庫填充RrecyclerView,但希望僅基於用戶標識查詢數據庫中的日期。我有應用程序的LoginActivty作爲全局變量分配的用戶ID,但我不知道該從哪裏將該信息從我的DateActivity傳遞到PHP頁面。通過Android Studio中的AsyncTask將全局變量傳遞給php文件

我的代碼爲DateActivity如下:

import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.widget.Toast; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.ArrayList; 
import java.util.List; 

public class DateActivity extends AppCompatActivity { 

public static String globex_num; 

// CONNECTION_TIMEOUT and READ_TIMEOUT are in milliseconds 
public static final int CONNECTION_TIMEOUT = 10000; 
public static final int READ_TIMEOUT = 15000; 
private RecyclerView mRVDateList; 
private AdapterDate mAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_date); 
    //Make call to AsyncTask 
    new AsyncFetch().execute(); 
} 

private class AsyncFetch extends AsyncTask<String, String, String> { 
    ProgressDialog pdLoading = new ProgressDialog(DateActivity.this); 
    HttpURLConnection conn; 
    URL url = null; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

     //this method will be running on UI thread 
     pdLoading.setMessage("\tLoading..."); 
     pdLoading.setCancelable(false); 
     pdLoading.show(); 

    } 

    @Override 
    protected String doInBackground(String... params) { 
     try { 

      url = new URL("thephpfile.com"); 

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

     try { 

      int response_code = conn.getResponseCode(); 

      // Check if successful connection made 
      if (response_code == HttpURLConnection.HTTP_OK) { 

       // Read data sent from server 
       InputStream input = conn.getInputStream(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(input)); 
       StringBuilder result = new StringBuilder(); 
       String line; 


       while ((line = reader.readLine()) != null) { 
        result.append(line); 
       } 

       // Pass data to onPostExecute method 
       return (result.toString()); 

      } else { 

       return ("unsuccessful"); 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
      return e.toString(); 
     } finally { 
      conn.disconnect(); 
     } 


    } 

    @Override 
    protected void onPostExecute(String result) { 

     //this method will be running on UI thread 

     pdLoading.dismiss(); 
     List<DataDate> data=new ArrayList<>(); 

     pdLoading.dismiss(); 
     try { 

      JSONArray jsonArray = new JSONArray(result); 

      // Extract data from json and store into ArrayList as class objects 
      for(int i=0;i<jsonArray.length();i++){ 
       JSONObject json_data = jsonArray.getJSONObject(i); 
       DataDate dateData = new DataDate(); 
       dateData.date= json_data.getString("date"); 
       data.add(dateData); 
      } 

      // Setup and Handover data to recyclerview 
      mRVDateList = (RecyclerView)findViewById(R.id.dateList); 
      mAdapter = new AdapterDate(DateActivity.this, data); 
      mRVDateList.setAdapter(mAdapter); 
      mRVDateList.setLayoutManager(new LinearLayoutManager(DateActivity.this)); 

     } catch (JSONException e) { 
      Toast.makeText(DateActivity.this, e.toString(), Toast.LENGTH_LONG).show(); 
     } 

    } 

} 
} 
+0

請不要傾倒你的整個代碼庫進入正題......只把部分(或多個)您認爲是與問題有關 –

回答

0

我通過傳遞它雖然鏈路解決了這個問題。

url = new URL(www.thephpfile.php?userID=" + LoginActivity.userID); 

在php文件的開始,我做了以下內容:

$userID = $_GET['userID'];