2

Actually I want to pass parameters in httpurlconnection get method. I am unable to do this as I am new in android. Can anyone please guide me how to send get request with parameters in android...please help me.

public AttemptSignIn(Context context) {
        this.context = context;
    }
    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        try{

            URL url = new URL("http://winktechs.com/windictor/sign_in.php");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            conn.setReadTimeout(10000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            //conn.setDoOutput(true);

            JSONObject jsonObj = new JSONObject();
            jsonObj.put("email", email.getText().toString());
            jsonObj.put("password", password.getText().toString());

            JSONArray jsonArray = new JSONArray();
            jsonArray.put(jsonObj);


            conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");


            conn.connect();
            int responseCode = conn.getResponseCode();

            Log.d("response", responseCode + "");

            if (responseCode==200) {
                inputStream = new BufferedInputStream(conn.getInputStream());
                String response = convertInputStreamToString(inputStream);
              //  parseResult(response);
                result = 1; // Successful
                flag = true;
            }
            else
            {
                flag = false;

            }

        }
        catch (JSONException ej)
        {
            flag = false;
            ej.printStackTrace();
        }

        catch (Exception e)
        {

            e.printStackTrace();
        }

        return null;
    }
    protected void onPostExecute(String file_url) {
        if(flag==true)
        {
            Toast.makeText(context, "Logged In Successfully !", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(SignIn.this,MainActivity.class);
            startActivity(intent);
            finish();
        }
        else
        {
            //Toast.makeText(context, Res.toString(),Toast.LENGTH_LONG).show();
        }

    }

}

private String convertInputStreamToString(InputStream inputStream) throws IOException {
    BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
    String line = "";
    String result = "";
    while((line = bufferedReader.readLine()) != null){
        result += line;
    }

        /* Close Stream */
    if(null!=inputStream){
        inputStream.close();
    }
    return result;
}
Mahendra Gunawardena
  • 1,956
  • 5
  • 26
  • 45
Usman Ahmed
  • 39
  • 3
  • 9
  • 1
    Simple logic: HTTP GET is not a proper method for POSTing parameters... – Selvin Mar 27 '16 at 10:43
  • so how to read data from server? can we do this with POST? – Usman Ahmed Mar 27 '16 at 10:45
  • you need to post data to your server in the URL like: http://winktechs.com/windictor/sign_in.php?id= and read them with GET – Kostas Drak Mar 27 '16 at 10:47
  • Obviously your question makes no sense... As you didn't include information what serverside expects to get... Also you should learn HTTP basics to understand what you can and what you can't - now talking with you about programming is like talking about colors with the blind man.... via GET you can pass parameters with query string... – Selvin Mar 27 '16 at 10:49
  • can anyone please expain a little bit through coding? – Usman Ahmed Mar 27 '16 at 10:57
  • Convert the JsonObject to JsonString and use it as parameter for you Get operation – Want2bExpert Mar 27 '16 at 11:48
  • I just post a complete working GET solution [here](http://stackoverflow.com/questions/36221795/android-parse-json-data-from-a-web-server-and-display-on-listview/36243380#36243380) I hope you will find this useful – Mahendra Gunawardena Mar 27 '16 at 12:21

1 Answers1

-3

It is recommended for security reasons to avoid using GET when sending sensitive data like email, password, phone number over http, use POST method instead at least.
Using Http POST: - convert your jsonobject to JsonString - use OutputStream to attach the parameters to the Post e.g.

JsonString sParams = jsonobject.toString(); 
// write parameter to post 
OutputStream urlParam = httppost.getOutputStream();
// use buffer 
BufferedWriter buff  = new BufferedWriter (new OutputStreamWriter (urlParam ));
buff.write (sParams);
buff.flush ();
buff.close ();
// close the Stream
UrlParam.close (); 
Want2bExpert
  • 527
  • 4
  • 11