Tôi đang tạo một thử nghiệm đơn vị để thử servlet mà tôi vừa tạo ra.Tạo một UrlEncodedFormEntity từ một danh sách NameValuePairs ném một NullPointerException
@Test
public void test() throws ParseException, IOException {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://localhost:8080/WebService/MakeBaby");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("father_name", "Foo"));
nameValuePairs.add(new BasicNameValuePair("mother_name", "Bar"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = null;
try {
response = client.execute(post);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String stringifiedResponse = EntityUtils.toString(response.getEntity());
System.out.println(stringifiedResponse);
assertNotNull(stringifiedResponse);
}
Các dòng sau tạo ra một NullPointerException:
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Có cái gì tôi đang thiếu?
Tôi nghĩ bạn có thể chấp nhận câu trả lời ngay bây giờ. Tôi đã có ngoại lệ tương tự và thêm mã hóa giải quyết vấn đề của tôi. Cảm ơn bạn. –
Tôi nghĩ rằng đây không phải là câu hỏi ngu ngốc với 10 như :), vấn đề của tôi cũng tương tự như bạn Cảm ơn. –
Điều này đã lưu ngày cho tôi: Tôi đã có giá trị utf-8 trong các cặp, và do đó chúng không được mã hóa chính xác - nó đã ném ký tự utf-8, như ISO-8859-1 sẽ làm! Cảm ơn! – rfay