在php android中传递2个变量
问题描述:
Good day, I have a php code that has a $_POST['Query'] function, i want to add it with $_POST['Tags'] but how can I pass 2 variables in android using to php, I had pass the a value to a $_POST['Query'] with my code but how can I pass the second one ?
OutputStream os = con.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
String data = URLEncoder.encode("Query", "UTF-8")+"="+URLEncoder.encode(query,"UTF-8");
bw.write(data);
bw.flush();
con.connect();
bw.close();
os.close();
美好的一天, 我有一个PHP代码,它有一个$ _POST ['Query']函数,我想要 用$ _POST ['Tags']添加它但是如何将android中的2个变量传递给php,我已经用我的代码将a值传递给$ _POST ['Query']但是如何传递第二个呢? p>
OutputStream os = con.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os,“UTF-8”));
String data = URLEncoder .encode(“查询”,“UTF-8”)+“=”+ URLEncoder.encode(查询,“UTF-8”);
bw.write(数据);
bw.flush();
con.connect();
bw.close();
os.close();
code> pre>
div>
答
Simply append another parameter with &
String data = URLEncoder.encode("Query", "UTF-8")+"="+URLEncoder.encode(query,"UTF-8");
data+= "&"+URLEncoder.encode("Tags", "UTF-8")+"="+URLEncoder.encode(yourTags,"UTF-8");
Note : For efficiency use StringBuilder
StringBuilder params = new StringBuilder();
params.append(URLEncoder.encode("Query", "UTF-8")+"="+URLEncoder.encode(query,"UTF-8"));
params.append("&");
params.append(URLEncoder.encode("Tags", "UTF-8")+"="+URLEncoder.encode(yourTagStr,"UTF-8"));
bw.write(params.toString());
// ... code