0

For a school project, I have to link a spinner with my database.

I've already succeeded in this step, but now they want the spinner to only show data in relation with the id of the person connected in my application.

I would like to know, how can I send the id in my PHP script?

I've tried this code, but when I launch the application after I've connected, my application crashes.

Here is my code :

package com.example.gardenounou;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class UserProfile extends Activity {

    EditText nom,prenom,harr,hdep,nbrepas,frais,frais_entretien,frais_transport,date_garde;
    String str_nom,str_prenom,str_harr,str_hdep,str_nbrepas,str_frais,str_fraisentretiens,str_fraistransport,str_date_garde;
    Button valider;
    ArrayList<String> listItems=new ArrayList<>();
    ArrayAdapter<String> adapter;
    Spinner sp;
    Intent intent = getIntent();
    String userId = intent.getStringExtra("id");
    @Override
    protected void onCreate(Bundle savedInstanceState)     {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_profile);


        harr = (EditText)findViewById(R.id.textharr);
        hdep = (EditText)findViewById(R.id.texthdep);
        nbrepas = (EditText)findViewById(R.id.textnbrrepas);
        frais = (EditText)findViewById(R.id.textfrais);
        valider =(Button)findViewById(R.id.btnvalider);
        frais_entretien = (EditText)findViewById(R.id.tfentretien);
        frais_transport = (EditText)findViewById(R.id.tftransport);
        sp=(Spinner)findViewById(R.id.spinnernom);
        adapter=new ArrayAdapter<String>(this,R.layout.spinner_nom,R.id.txt,listItems);
        sp.setAdapter(adapter);

    }
    public void onStart(){
        super.onStart();

        BackTask(userId);

     }
    private void  BackTask (final String idnounou) {
         @SuppressWarnings("unused")
        class BackTaskClass extends AsyncTask<Void,String,Void>{
        ArrayList<String> list;
        protected void onPreExecute(){
          super.onPreExecute();
          list=new ArrayList<>();
        }
        protected String doInBackground(String...params){
            InputStream is=null;
            String result="";
            String id_nounou = params[0];
            String login_url = "http://192.168.20.135/scriptphp/listinfos.php";
            try{
                 URL url = new URL(login_url);
                 HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                 httpURLConnection.setRequestMethod("POST");
                 httpURLConnection.setDoOutput(true);
                 httpURLConnection.setDoInput(true);
                 OutputStream outputStream = httpURLConnection.getOutputStream();
                 BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                 String post_data = URLEncoder.encode("id_nounou", "UTF-8")+"="+URLEncoder.encode(id_nounou,"UTF-8");
               // Get our response as a String.
                 bufferedWriter.write(post_data);
                 bufferedWriter.flush();
                 bufferedWriter.close();
                 outputStream.close();
                 InputStream inputStream = httpURLConnection.getInputStream();      

            //convert response to string

               BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));           
               String line = null;
               while ((line = reader.readLine()) != null) {
                 result+=line;
               }
               reader.close();
               inputStream.close();
               httpURLConnection.disconnect();

                             //result=sb.toString();
            }catch(Exception e){
               e.printStackTrace();
            }
            // parse json data
            try{
               JSONArray jArray =new JSONArray(result);
               for(int i=0;i<jArray.length();i++){
                  JSONObject jsonObject=jArray.getJSONObject(i);
                  // add interviewee name to arraylist
                  list.add(jsonObject.getString("nom_enfant"));
               }
            }
            catch(JSONException e){
               e.printStackTrace();
            }
            return null;
          }
          protected void onPostExecute(Void result){
            listItems.addAll(list);
            adapter.notifyDataSetChanged();
          }
        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
            return null;
        }
         }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    public void Onreg(View view)  
    {
        str_nom = nom.getText().toString();
        str_prenom = prenom.getText().toString();
        str_harr = harr.getText().toString();
        str_hdep = hdep.getText().toString();
        str_nbrepas = nbrepas.getText().toString();
        str_frais = frais.getText().toString();
        str_fraistransport = frais_transport.getText().toString();
        str_fraisentretiens = frais_entretien.getText().toString();
        String type= "infogarde";
        BackgroundWorker registermain = new BackgroundWorker(this);
        registermain.execute(type,str_nom,str_prenom,str_harr,str_hdep,str_nbrepas,str_frais,str_fraisentretiens,str_fraistransport);

    }
}

and here is my script php :

<php
    require_once('dbconnect');
    $id_nounou = $_POST['id_nounou];

    $sql="SELECT id_parent,id_enfant,nom_enfant FROM enfants where id_nounou =" .'$id_nounou;
    $result=$mysqli->query($sql);
    while($e=mysqli_fetch_assoc($result)) {
        $output[]=$e; 
    }

    print(json_encode($output)); 
    $mysqli->close();   

?>  
António Ribeiro
  • 3,962
  • 4
  • 30
  • 45

0 Answers0