如何将变量从 Android 发送到 PHP 以执行 SELECT ... WHERE in PHP

How to send variables to PHP from Android to do a SELECT ... WHERE in PHP

我已经找到了从 mysql 读取数据并将其显示到我的 android 应用程序的文本视图中的方法。问题是现在我只是从数据库中检索所有数据而没有执行 select ... 其中,只是 select。我想做一个 select ... 使用从我的 android 应用程序发送的变量。我已经在登录中使用以下代码发送用户名并传递

nameValuePairs=new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("utilizator",utilizator.getText().toString().trim())); nameValuePairs.add(new BasicNameValuePair("parola",parola.getText().toString().trim())); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

在我使用的方法中,我有 2 个 java 文件。我真的不太了解他们......所以我不知道在哪里添加发送变量的代码到php。

httprequest.java

public class httprequest {
    static int TIMEOUT = 5000;

    public String read_url_resource(String _s_url)
    {
        String          s_response_ = "";
        BufferedReader br_reader=null;
        StringBuilder   sb_builder = new StringBuilder();

        URL u_url = null;
        try {
            if ( _s_url==null || _s_url.length()==0)
                return "";

            //this.log_message("Downloading from "+_s_url, false);
            u_url = new URL(_s_url);

            HttpURLConnection huc_urlConnection = null;


            huc_urlConnection = (HttpURLConnection)u_url.openConnection();

            if ( huc_urlConnection!=null){
                huc_urlConnection.setConnectTimeout(TIMEOUT);
                huc_urlConnection.setReadTimeout(TIMEOUT);
                huc_urlConnection.setUseCaches(false);
                huc_urlConnection.setDoOutput(false);
                InputStream is_input_stream = huc_urlConnection.getInputStream();
                @SuppressWarnings("unused")
                URL oURL = huc_urlConnection.getURL();

                int i_response_code=huc_urlConnection.getResponseCode();
                if ( i_response_code==200){
                    br_reader = new BufferedReader(new InputStreamReader(is_input_stream));

                    String line="";
                    while ((line = br_reader.readLine()) != null)
                        sb_builder.append(line);


                    is_input_stream.close();
                    s_response_ = sb_builder.toString();

                    String s_auth= huc_urlConnection.getURL().getAuthority();
                }
            }
        } catch (Exception e) {
            if ( e!=null )
                Log.e("URL PROBLEM", e.toString());
            s_response_ = "[\"error\": \"No connection\"]";
        }

        return s_response_;
    }
}

和ReadJSONData.java

public class ReadJSONData extends AsyncTask<String, Integer, Integer> {

    private String server = "http://asociatia-online.esy.es/cote.php?hc_location=ufi";
    private ReadJSONListener jsonListener;
    private String json ="";
    private int internal_categ;
    httprequest request;

    public interface ReadJSONListener
    {
        void onTaskFinished(String s_json, int _i_internal_category);
    }

    public ReadJSONData (ReadJSONListener _jsonListener, int _i_internal_category)
    {
        this.jsonListener=_jsonListener;
        this.internal_categ =_i_internal_category;
    }

    @Override
    protected Integer doInBackground(String... strings) {
        request = new httprequest();
        this.downloadResource();
        return null;
    }

    private void downloadResource() {
        switch (this.internal_categ) {
            case 1:
                this.json = request.read_url_resource(server);
                break;
        }
    }

    @Override
    protected void onPostExecute(Integer result)
    {
        this.jsonListener.onTaskFinished(this.json, this.internal_categ);
    }
}

那么,发送变量的4行代码在哪里添加呢?在哪个文件和哪里。谢谢!

使用此代码,您可以将变量发送到 PHP 并取回一些数据并将其存储在字符串中以供您使用。

private class ObtinereInformatii extends AsyncTask<String, Void, String>{

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

            //http post
            try {
                HttpClient httpclient = new DefaultHttpClient();


                HttpPost httppost = new HttpPost("http://asociatia-online.esy.es/rezumat.php");
                nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair("luna_rezumat",spinner_luna.getSelectedItem().toString().trim()));
                nameValuePairs.add(new BasicNameValuePair("an_rezumat",spinner_an.getSelectedItem().toString().trim()));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
            } catch (Exception e) {
                Log.e("log_tag", "Error in http connection" + e.toString());
            }
//convert response to string
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                sb = new StringBuilder();
                sb.append(reader.readLine() + "\n");

                String line = "0";
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                result = sb.toString();
                Log.e("-------",result);
            } catch (Exception e) {
                Log.e("log_tag", "Error converting result " + e.toString());
            }

            return result;
        }

        protected void onPostExecute(String result){

                try{
                jArray = new JSONArray(result);

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

    }

    private void readJson(JSONArray ja_data) throws JSONException {
        if (ja_data!=null)
        {
            for (int i = 0; i<ja_data.length(); i++)
            {
                JSONObject jo = ja_data.getJSONObject(i);
                if (jo.has("data_rezumat"))
                {
                    if (jo.getString("data_rezumat")!=null)
                    {
                        z_data_rezumat= jo.getString("data_rezumat");
                    }
                }
            }
        }
    }