我正在创建一个应用程序,必须在其中向服务器发送或发布数据。我能够将数据发送到对象或数组,但是我不能先在对象中发送数据,而不是在其子数组中发送数据,然后再发送对象。有人能帮助我解决这个问题吗?我将非常感激。数据以下面给出的形式发送。
{
name:
no:
data: [
{
surname:
option:
another_option:
}
]
another:
}
我想以字符串形式发布我的数据,因为它是以对象形式发布的,我想更新其中一个对象中的ArrayList,但是我无法获取如何在“ data:”中发送数据。获取如何在参数中的数组列表中发布我的字符串值。不能有n。我的ArrayList对象中的数组列表。我无法获取如何发送数据或将数据放入POST方法,请帮助我]
这是我正在尝试的代码。
private void new_process() {
String URL = "http://117.240.196.238:8080/api/raid";
Log.i("response", URL);
StringRequest jsonObjRequest = new StringRequest(
Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("response_process", response);
Upload_work(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("volley", "Error" + error.getMessage());
Log.i("response_error", error.toString());
}
}) {
@Override
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=UTF-8";
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
String lic_date, lic_letter, lic_copy, sale_date, sale_letterno, sale_copy, material_name, material_quantity, quantity_in, fir_num, fir_dates, fir_policestation, sample_code, sample_date; List<String> number = new ArrayList<>();
number.add(first_text);
number.add(second_text);
number.add(third_text);
params.put("name", name);
params.put("no.", number);
params.put("data", ?????); // how to pass my list of string number to this data
params.put("another", another);
return params;
}
};
RequestQueue queue = SingletonRequestQueue.getInstance(getApplicationContext()).getRequestQueue();
queue.add(jsonObjRequest);
}
private void Upload_work(String response) {
Log.i("response_Upload_work", response);
try {
JSONObject json = new JSONObject(response);
int success = json.getInt("success");
String msg = json.getString("message");
if (success == 1) {
JSONObject c = json.getJSONObject("data");
String messg = c.getString("message");
Toast.makeText(this, messg, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(ScrollingActivity.this, msg, Toast.LENGTH_LONG).show();
}
} catch (Exception ex) {
Log.i("responseNew m", "Exception : " + ex.toString());
}
}
我尝试创建一个JsonArray,然后创建一个Jsonobject,然后在我的参数中传递json数组,但我无法将jsonArray和jsonobject链接在一起
JsonArray sample_obj = new JsonArray();
try {
JSONObject j = new JSONObject();
JSONObject c = new JSONObject(j.toString());
c.put("name", sample_code);
c.put("value", "google.com");
} catch (JSONException e) {
e.printStackTrace();
}
[如果我以正确的方式执行此操作或需要执行其他操作,请帮助我。
JSONArray jsonArray = new JSONArray();
for(**:**:**){
JSONObject jsOb = new JSONObject();
jsOb.put("a", a);
jsOb.put("b", b);
jsOb.put("c", c);
jsonArray.put(jsOb);
}
jQueue = Volley.newRequestQueue(getApplicationContext());
String jurl = URL---------------------;
final UserLocalStore userLocalStore = new UserLocalStore(getApplicationContext());
JSONObject jsonObject= new JSONObject();
try {
jsonObject.put("x", x);
jsonObject.put("y", y);
jsonObject.put("z", z);
jsonObject.put("array", jsonArray);
}catch (JSONException e){
e.printStackTrace();
}
//Log.d("check param",jsonObject.toString());
final JsonObjectRequest jrequest = new JsonObjectRequest(jurl, jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("Volley",response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("**VolleyError", "error" + error.getMessage());
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return super.getParams();
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
CurrentUser user = userLocalStore.getLoggedInUser();
String credentials = user.getUsername()+":"+user.getPassword();
String auth = "Basic "
+ Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
headers.put("Content-Type", "application/json");
headers.put("Authorization", auth);
return headers;
}
};
jQueue.add(jrequest);
}
请参见上面的示例
您应该使用JSONArray
import org.json.JSONArray;
不是JsonArray
import com.google.gson.JsonArray;