【问题标题】:How to send get request with parameters in httpurlconnection?如何在httpurlconnection中发送带有参数的get请求?
【发布时间】:2016-03-27 10:38:18
【问题描述】:

其实我想在httpurlconnection get 方法中传递参数。我无法做到这一点,因为我是 android 新手。谁能指导我如何在android中发送带有参数的get请求...请帮助我。

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;
}

【问题讨论】:

    标签: android httpurlconnection


    【解决方案1】:

    出于安全原因,建议在通过 http 发送电子邮件、密码、电话号码等敏感数据时避免使用 GET,至少使用 POST 方法。
    使用 Http POST: - 将您的 jsonobject 转换为 JsonString - 使用 OutputStream 将参数附加到 Post,例如

    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 (); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-18
      • 2018-01-25
      • 2020-09-11
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多