Home >Java >javaTutorial >Example tutorial on passing parameters using NameValuePair method

Example tutorial on passing parameters using NameValuePair method

零下一度
零下一度Original
2018-05-30 14:39:123912browse

Today, during joint debugging, an external interface was transmitted using POST mode. I encapsulated the parameters as Jason strings and passed them in according to the document, but the other party kept accepting the parameters as empty. After struggling for a long time, I still couldn't find the problem. I'm very distressed. There are no errors when checking the code, but why does the other party accept empty parameters? Then I contacted the other party's technical staff for joint debugging to see what was going on. After struggling for a long time, I finally found that the other party was using the NameValuePair method to pass parameters. Although this method is outdated, it is recorded here in case a similar method of passing parameters appears in the future.

 1 /** 
 2 /** 
 3  * 定义了一个list,该list的数据类型是NameValuePair(简单名称值对节点类型), 
 4  * 这个代码用于Java像url发送Post请求。在发送post请求时用该list来存放参数。 
 5  */ 
 6  
 7 List<NameValuePair> urlParameters = new ArrayList<>(); 
 8 urlParameters.add(new BasicNameValuePair("token", token)); 
 9 urlParameters.add(new BasicNameValuePair("city", city));
 10 urlParameters.add(new BasicNameValuePair("timestamp", timestamp));
 11 urlParameters.add(new BasicNameValuePair("sign", sign));
 12 ...
 13 
 14 HttpPost post = new HttpPost("http://****:8047/v4.0/quoted");//建立HttpPost对象
 15 post.setEntity(new UrlEncodedFormEntity(urlParameters, HTTP.UTF_8));//设置编码
 16 HttpResponse response=new DefaultHttpClient().execute(post);//发送Post,并返回一个HttpResponse对象
 17 String content = EntityUtils.toString(response.getEntity(), "UTF-8");

The above is the detailed content of Example tutorial on passing parameters using NameValuePair method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn