简体   繁体   中英

Http Parameters as Json

One of the requests on my page has some parameters and in Firefox DevTool it looks like: 在此处输入图片说明

How is it possible to create this structure in url or just add parameters in Java?

Usually I use Apache HttpPost and then:

 ArrayList<NameValuePair> postParameters;
  postParameters = new ArrayList<NameValuePair>();
  postParameters.add(new BasicNameValuePair("key", "value"));
  httpPost.setEntity(new UrlEncodedFormEntity(postParameters, "UTF-8"));

but have no idea how to pass json as my request parameter...

@Update

So in my example should it looks like:

Map<String, Object> comMap = new HashMap<String, Object>();
Map<String, Object> iMap = new HashMap<String, Object>();
Map<String, Object> mainMap = new HashMap<String, Object>();

comMap.put("p","A");

iMap.put("com", comMap);
iMap.put("t", "b");

mainMap.put("ex", null);
mainMap.put("i(2)", null);
mainMap.put("i(3)", iMap);

where i(2) is i from second line and i(3) is i from third line? How is it possible to add map to HttpPost (if it is correct)?

@Update

I send requests by using:

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost;
httpClient = new DefaultHttpClient();
httpPost = new HttpPost("myUrl");
httpPost.setHeader("headerKey","headerValue");

ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("paramKey", "false"));
httpPost.setEntity(new UrlEncodedFormEntity(postParameters, "UTF-8"));
HttpResponse response = httpClient .execute(httpPost);

Basic Map acts as a key => value , something like this:

ArrayList<NameValuePair> comList = new ArrayList<NameValuePair>();
ArrayList<NameValuePair> iList = new ArrayList<NameValuePair>();
ArrayList<NameValuePair> mainList = new ArrayList<NameValuePair>();

comList.add(new BasicNameValuePair("p", "A");

iList.add(new BasicNameValuePair("com", comList);
iList.add(new BasicNameValuePair("t", "b"));

mainList.add(new BasicNameValuePair("ex", null));
mainList.add(new BasicNameValuePair("i(2)", null));
mainList.add(new BasicNameValuePair("i(3)", iList));

You can use GSON library and define a class with every field on it like below

Class Car { String name; String color;}

and pass data to it post this class. or you can follow this link

HTTP POST using JSON in Java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM