我想在一个键中发送多个值,例如sendTo [] -string数组和值分隔的值,如abcd @ gmail.com,pqrs @ gmail.com。
我尝试使用字符串构建器,使用逗号分隔附加所有值,然后使用键在哈希映射中添加构建器。
像这样:
StringBuilder emailbuilder = new StringBuilder();
for (String mail : mEmailList) {
emailbuilder.append(mail+",");
i++;
}
data.put("send_to[]", emailbuilder.toString());
所以它导致hashmap为abcd @ gmail.com,pqrs @ gmail.com, - 像这样的字符串被添加为关键字send_to []的值。
后来,数据发送的类型是x-www-form-urlencoded,所以我将数据转换为该格式。
要转换的功能:
private String getDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
但是,转换后它也会转换值。 send_to []键值变为--- &send_to%5B%5D=realdammy%40hotmail.com%2Csiddhijambhale%40gmail.com%2C
它将括号和逗号转换为%格式。我需要发送数据,如--- send_to key和value abcd @ gmail.com,pqrs @ gmail.com
我怎样才能做到这一点?请帮忙谢谢......
编辑:运行请求的功能。
public String sendPostRequest(String data) {
try {
URL url = new URL(api);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("content-type", "application/x-www-form-urlencoded");
con.setDoOutput(true);
con.setDoInput(true);
OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
try {
writer.write(data);
} catch (Exception e) {
Log.e("Exception111", e.toString());
}
writer.close();
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED) {
StringBuilder sb = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line);
}
reader.close();
Log.d("ServerResponse", new String(sb));
String output = new String(sb);
return output;
} else {
Log.e("Exception", "" + responseCode);
}
}
catch(IOException e)
{
Log.e("Exception", "" + e.toString());
}
return null;
}
答案 0 :(得分:0)
当您在GET请求中发送值时,您无法实现的目标。无论如何,这些值将被编码为%表格,以便与GET请求一起发送。只有在POST请求中发送值时才能执行此操作。
要在当前上下文中回答,您需要在后端执行URLDecode以获取该表单中的值。
这是非常正常的事情,可能甚至不需要任何额外的后端编程。您应该轻松收到相同的private String getDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first)
first = false;
else
result.append("\n");
result.append(entry.getKey());
result.append(":");
result.append(entry.getValue());
}
return result.toString();
}
让我知道这是否有效......
编辑1:
通常我们会像Volley这样的库使用这些东西..
但要使用HttpURLConnection执行此操作,请使用以下方法准备数据字符串
data.put("sent_to", TextUtils.join(",",mEmailList));
使用以下方法将电子邮件添加到数据
xbook = xlsxwriter.Workbook('abcd.xlsx')
xsheet1 = xbook.add_worksheet('img_reshape')
xsheet2 = xbook.add_worksheet('column_sum')
xsheet3 = xbook.add_worksheet('row_sum')
for col,array in enumerate (dataset):
for row,value in enumerate (array):
xsheet1.write(row, col, value)
for col,array in enumerate (column_sum):
for row,value in enumerate (array):
xsheet2.write(row, col, value)
for col,array in enumerate (row_sum):
for row,value in enumerate (array):
xsheet3.write(row, col, value)
xbook.close()
让我发布...
答案 1 :(得分:0)
你可以使用凌空https://github.com/google/volley。
String tag_json_obj = "json_obj_req";
String url = "your url";
ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
pDialog.hide();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("send", "abc@gmail.info,abc1@gmail.info");
params.put("password", "password123");
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);