我正在尝试将httpPost写入Web服务。我想将列表作为数组发送。这是我到目前为止所做的。
public Response searchMessages(@QueryParam("tags") List<String> tags, @QueryParam("senders") List<String> senders, @QueryParam("api_keys") List<String> apiKeys) throws Exception
{Iterator<String> tagsIt = tags.iterator();
Iterator<String> sendersIt = senders.iterator();
Iterator<String> apiKeysIt = apiKeys.iterator();
CloseableHttpClient httpClient = null;
HttpPost httpPost =null;
List<NameValuePair> nvps=null;
CloseableHttpResponse response=null;
InputStream in=null;
String myResponse = "";
try
{
httpClient = HttpClients.createDefault();
httpPost = new HttpPost(url);
while(tagsIt.hasNext())
{
String keyIt = tagsIt.next();
nvps.add(new BasicNameValuePair("tags",hashMap.get(keyIt)));
}
while(sendersIt.hasNext())
{
String keySend = sendersIt.next();
nvps.add(new BasicNameValuePair("senders",hashMap.get(keySend)));
}
while(apiKeysIt.hasNext())
{
String keySend = sendersIt.next();
nvps.add(new BasicNameValuePair("apiKeys",hashMap.get(keySend)));
}
//System.out.println(key+" "+hashMap.get(key));
httpPost.setEntity(new UrlEncodedFormEntity(nvps,Consts.UTF_8));
response = httpClient.execute(httpPost);
BufferedReader buffer=null;
try{
//System.out.println(response.toString());
in= response.getEntity().getContent();
buffer = new BufferedReader(new InputStreamReader(in));
String s = "";
while ((s = buffer.readLine()) != null) {
myResponse += s+"\n";
}
status= response.getStatusLine().getStatusCode();
resp = Response.status(status).entity(myResponse).build();
}
catch(Exception e)
{
e.printStackTrace();
}
}
finally
{
in.close();
response.close();
httpClient.close();
}
我的问题是我在in.close()行中得到一个空指针异常。 我不知道我的错误在哪里。请帮助。