通过Android Studio中的AsyncTask将全局变量传递给php文件

时间:2017-03-28 13:24:18

标签: java android android-asynctask

我目前正在从远程数据库填充RrecyclerView,但希望仅根据用户ID查询数据库中的日期。我在应用程序的LoginActivty上将userID指定为全局变量,但我不确定从DateActivity将信息传递到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();
        }

    }

}
}

1 个答案:

答案 0 :(得分:0)

我通过链接传递了问题。

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

在php文件的开头,我做了以下事情:

$userID = $_GET['userID'];
相关问题