2017-03-28 11 views
0

現在、リモートデータベースからRrecyclerViewにデータを入力していますが、ユーザIDに基づいてデータベースのクエリを実行するだけです。私は、アプリケーションのLoginActivtyにグローバル変数として割り当てられたuserIDを持っていますが、私はDateActivityからphpページにその情報を渡す場所がわかりません。次のようにDateActivityためAndroidスタジオのAsyncTaskを使ってグローバル変数をPHPのファイルに渡す

マイコードは次のとおりです。

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

私はリンクかかわらず、それを渡すことによって、問題を解決しました。私は次のことをやったPHPファイルの開始時に

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

$userID = $_GET['userID']; 
関連する問題