2016-08-23 6 views
1

私はアプリケーションを開発しています。ここでは共有の設定を使用してセッションを維持したいと思います。この次のページではセッションを提供していますが、現在のセッションのメールを現在のユーザーのデータを取得するPHPファイルで使いたいですこのセッション電子メールをPHPファイルに取得しますか?これを解決する方法を提案してください。どのように私はPHPにセッションのメールを与えるのですか?

//java file 

public class DetailsActivity2 extends Activity { 
      TextView uid; 
      TextView name1, address1, seats1, email1; 
      TextView amount1; 
      Button Btngetdata; 
      private boolean loggedIn = false; 

      //URL to get JSON Array 
     private static String url = "http://example.in/ticket1.php"; 

      //JSON Node Names 
      private static final String TAG_USER = "result"; 
      private static final String TAG_NAME = "pname"; 
      private static final String TAG_AMOUNT = "pamount"; 
      private static final String TAG_ADDRESS = "paddress"; 
      private static final String TAG_SEATS = "pseats"; 

      JSONArray result = null; 

      @Override 
      protected void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 

       setContentView(R.layout.details_activity); 
       Btngetdata = (Button)findViewById(R.id.button3_submit); 
       email1=(TextView)findViewById(R.id.textView_email); 

       new JSONParse().execute(); 

       Btngetdata.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 

         SharedPreferences sharedPreferences = getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE); 
         loggedIn = sharedPreferences.getBoolean(Config.LOGGEDIN_SHARED_PREF, false); 
         String email = sharedPreferences.getString(Config.EMAIL_SHARED_PREF, "Not Available"); 
         email1.setText(email); 

         if(loggedIn){ 

          Intent intent = new Intent(DetailsActivity2.this, LoginActivity.class); 
          startActivity(intent); 
         } 
        } 
       }); 
      } 

      private class JSONParse extends AsyncTask<String, String, JSONObject> { 
       private ProgressDialog pDialog; 
       @Override 
       protected void onPreExecute() { 
        super.onPreExecute(); 
       // uid = (TextView)findViewById(R.id.uid); 
        name1 = (TextView)findViewById(R.id.textView_name1); 
        amount1 = (TextView)findViewById(R.id.textView_amount1); 
        address1= (TextView)findViewById(R.id.textView_address1); 
        seats1= (TextView)findViewById(R.id.textView_sname1); 

        pDialog = new ProgressDialog(DetailsActivity2.this); 
        pDialog.setMessage("Getting Data ..."); 
        pDialog.setIndeterminate(false); 
        pDialog.setCancelable(true); 
        pDialog.show(); 

       } 

       @Override 
       protected JSONObject doInBackground(String... args) { 
        JSONParser jParser = new JSONParser(); 

        // Getting JSON from URL 
        JSONObject json = jParser.getJSONFromUrl(url); 
        return json; 
       } 
       @Override 
       protected void onPostExecute(JSONObject json) { 
        pDialog.dismiss(); 
        try { 
         // Getting JSON Array 
         result = json.getJSONArray(TAG_USER); 
         JSONObject c = result.getJSONObject(0); 

         // Storing JSON item in a Variable 
        // String id = c.getString(TAG_ID); 
         String name = c.getString(TAG_NAME); 
         String amount = c.getString(TAG_AMOUNT); 
         String seats = c.getString(TAG_SEATS); 
         String address = c.getString(TAG_ADDRESS); 



         //Set JSON Data in TextView 
         // uid.setText(id); 
         name1.setText(name); 
         address1.setText(address); 
         seats1.setText(seats); 
         amount1.setText(amount); 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      } 
     } 

//php file   
     <?php 

     $id= $_GET['email']; 
     $sql = "SELECT * FROM tbl_users WHERE email='$id'"; 

     $con=mysqli_connect("localhost","user","pass","db"); 

     $r =mysqli_query($con,$sql); 
     $result =array(); 

     while($row =mysqli_fetch_array($r)){ 
      array_push($result,array(
       'pname'=>$row['pname'], 
       'paddress'=>$row['paddress'], 
       'pseats'=>$row['pseats'], 
       'pamount'=>$row['pamount'] 
      )); 
     } 

     echo json_encode(array('result'=>$result)); 
     mysqli_close($con); 
     ?> 
+0

あなたはここでセッションを持っていません。あなたは「セッション」をどこで使っていますか? – Vyacheslav

+0

あなたは 'json'の結果で 'get'リクエストを送信します – Vyacheslav

+0

私はセッションにメールを送ります – rohiH

答えて

1
public class FileUploader { 
    private static final String LINE_FEED = "\r\n"; 
    private final String boundary; 
    private HttpsURLConnection httpConn; 
    private String charset; 
    private OutputStream outputStream; 
    private PrintWriter writer; 

    public FileUploader(String requestURL, String charset) 
      throws IOException { 
     this.charset = charset; 

     // creates a unique boundary based on time stamp 
     boundary = "===" + System.currentTimeMillis() + "==="; 

     URL url = new URL(requestURL); 
     httpConn = (HttpsURLConnection) url.openConnection(); 
     httpConn.setUseCaches(false); 
     httpConn.setDoOutput(true); // indicates POST method 
     httpConn.setDoInput(true); 
     httpConn.setRequestMethod("POST"); 
     httpConn.setRequestProperty("Content-Type", 
       "multipart/form-data; boundary=" + boundary); 

     httpConn.setRequestProperty("User-Agent", "CodeJava Agent"); 
     outputStream = httpConn.getOutputStream(); 
     writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), 
       true); 
    } 

    /** 
    * Adds a form field to the request 
    * 
    * @param name field name 
    * @param value field value 
    */ 
    public void addFormField(String name, String value) { 
     writer.append("--").append(boundary).append(LINE_FEED); 
     writer.append("Content-Disposition: form-data; name=\"").append(name).append("\"") 
       .append(LINE_FEED); 
     writer.append("Content-Type: text/plain; charset=").append(charset).append(
       LINE_FEED); 
     writer.append(LINE_FEED); 
     writer.append(value).append(LINE_FEED); 
     writer.flush(); 
    } 

    /** 
    * Adds a upload file section to the request 
    * 
    * @param fieldName name attribute in <input type="file" name="..." /> 
    * @param uploadFile a File to be uploaded 
    * @throws IOException 
    */ 
    public void addFilePart(String fieldName, File uploadFile) 
      throws IOException { 
     String fileName = uploadFile.getName(); 
     writer.append("--").append(boundary).append(LINE_FEED); 
     writer.append("Content-Disposition: form-data; name=\"").append(fieldName).append("\"; filename=\"").append(fileName).append("\"") 
       .append(LINE_FEED); 
     writer.append("Content-Type: ").append(URLConnection.guessContentTypeFromName(fileName)) 
       .append(LINE_FEED); 
     writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED); 
     writer.append(LINE_FEED); 
     writer.flush(); 

     FileInputStream inputStream = new FileInputStream(uploadFile); 
     byte[] buffer = new byte[4096]; 
     int bytesRead = -1; 
     while ((bytesRead = inputStream.read(buffer)) != -1) { 
      outputStream.write(buffer, 0, bytesRead); 
     } 
     outputStream.flush(); 
     inputStream.close(); 

     writer.append(LINE_FEED); 
     writer.flush(); 
    } 

    /** 
    * Adds a header field to the request. 
    * 
    * @param name - name of the header field 
    * @param value - value of the header field 
    */ 
    public void addHeaderField(String name, String value) { 
     writer.append(name).append(": ").append(value).append(LINE_FEED); 
     writer.flush(); 
    } 

    /** 
    * Completes the request and receives response from the server. 
    * 
    * @return a list of Strings as response in case the server returned 
    * status OK, otherwise an exception is thrown. 
    * @throws IOException 
    */ 
    public String finish() { 
     String response = ""; 

     writer.append(LINE_FEED).flush(); 
     writer.append("--").append(boundary).append("--").append(LINE_FEED); 
     writer.close(); 

     // checks server's status code first 
     int status = 0; 
     try { 
      status = httpConn.getResponseCode(); 

      if (status == HttpsURLConnection.HTTP_OK) { 
       BufferedReader reader = new BufferedReader(new InputStreamReader(
         httpConn.getInputStream())); 
       String line = null; 
       while ((line = reader.readLine()) != null) { 
        response += line; 
       } 
       reader.close(); 
       httpConn.disconnect(); 
      } else { 
       return null; 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
      return null; 
     } 

     return response; 
    } 
} 

その後

FileUploader fileUploader = new FileUploader("http://example.in/ticket1.php","UTF-8"); 
fileUploader.addFormField("email","[email protected]"); 
String response = fileUploader.finish(); 
関連する問題