发送URL POST并在BufferredReader中获得答案,响应以ASCII格式显示。如何将传入的字符串分解成碎片并仅使用我需要的字符串?
params.put("ssl_result_format", "ASCII");
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
Receipt pp = new Receipt();
pp.setVisible(true);
while ((line = in.readLine()) != null) {
Receipt.receText.append(line + "\n");
System.out.println(line);
这是显示方式:
ssl_card_number=50**********3003
ssl_exp_date=1215
ssl_amount=12.00
ssl_invoice_number=
ssl_departure_date=
ssl_completion_date=
ssl_issue_points=
ssl_promo_code=
ssl_enrollment=
ssl_result=0
ssl_result_message=APPROVAL
ssl_txn_id=051214A15-6E33E984-7C6B-466D-B38C-83F24BDAC631 ETC......
编辑:我完成了这件事:
Properties prop = new Properties();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
prop.load(in);
String amount = prop.getProperty("ssl_amount");
String card = prop.getProperty("ssl_card_number");
String approval = prop.getProperty("ssl_approval_code");
String results = prop.getProperty("ssl_result_message");
String time = prop.getProperty("ssl_txn_time");
String errorCode = prop.getProperty("errorCode");
String errorMessage = prop.getProperty("errorMessage");
String errorName = prop.getProperty("errorName");
if(errorCode == null){
Receipt pp = new Receipt();
pp.setVisible(true);
Receipt.amountLabel.setText(amount);
Receipt.cardLabel.setText(card);
Receipt.approvalLabel.setText(approval);
Receipt.respondLabel.setText(results);
Receipt.dateLabel.setText(time);
}else{
JOptionPane.showMessageDialog(null,errorName,errorMessage,JOptionPane.ERROR_MESSAGE);
}
+1来Crunchify.com
答案 0 :(得分:0)
在您的while循环中,检查该行是否以您关心的内容开头。例如,
while ((line = in.readLine() != null) {
if (line.startsWith("ssl_amount") {
Receipt.receText.append(line + "\n");
System.out.println(line);
}
}
答案 1 :(得分:0)
使用StringTokenizer打破字符串并检查字符串的第一部分,当你得到你想要打印的值时
params.put("ssl_result_format", "ASCII");
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
Receipt pp = new Receipt();
pp.setVisible(true);
while ((line = in.readLine()) != null) {
Receipt.receText.append(line + "\n");
StringTokenizer strToken = new StringTokenizer(line, "=");
if(strToken.hasMoreTokens() && strToken.nextToken().equals("ssl_amount"))
System.out.println(strToken.nextToken());