其实我想在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;
}
答案 0 :(得分:-3)
出于安全原因,建议在通过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 ();