我在Java Swing中创建了一个GUI,想要点击一个API(POST方法)点击一下按钮
我必须发送一个JSON对象作为请求参数以及标题,即(Headers [“custom-Header”] ==“AXYZ”)我已将我的字符串转换为JSON对象喜欢
{"machineKey":"","serialNumber":"","name":"","mobile":"","productKey":"","email":""}
我只是做了
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == Submit)
{
dispose();
new Otp().setVisible(true);
}
else
{
name.setText("");
email.setText("");
mobile.setText("");
machineKey.setText("");
productKey.setText("");
serialNumber.setText("");
}
String Sname= name.getText();
String Semail= email.getText();
String Smobile= mobile.getText();
String SmachineKey= machineKey.getText();
String SproductKey= productKey.getText();
String SserialNumber= serialNumber.getText();
JSONObject abc=prepareReqJsonObj(Sname,Semail,Smobile,SmachineKey,SproductKey,SserialNumber);
System.out.println(abc);
}
现在,我如何直接从我的GUI按钮发出命令来命中API,我可以通过什么方式实现这一点并获得响应。
提前感谢您的帮助:)
答案 0 :(得分:2)
查看this example如何使用HttpClient发布数据。
基本上,您创建一个HttpClient,创建一个POST请求,用您的数据填充该请求,然后提交。
答案 1 :(得分:1)
将此添加为所有帖子请求的通用实用程序类
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Utility {
public static String excutePost(String targetURL, String urlParameters)
{
URL url;
HttpURLConnection connection = null;
try {
//Create connection
url = new URL(targetURL);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/json");
connection.setRequestProperty("custom-Header", "XYZ");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if(connection != null) {
connection.disconnect();
}
}
}
}
和Action Listener中的这个 - >
String Sname= name.getText();
String Semail= emailid.getText();
String Smobile= mobile.getText();
String SmachineKey= machineKey.getText();
String SproductKey= productkey.getText();
String SserialNumber= serialNo.getText();
JSONObject reqObj=prepareReqJsonObj(Sname,Semail,Smobile,SmachineKey,SproductKey,SserialNumber);
String reqString= reqObj.toString();
String APIUrl= "http://example.com/v1/api";
String response= Utility.excutePost(APIUrl, reqString);
System.out.println(reqObj);
System.out.println(reqString);
System.out.println(response);
}
@SuppressWarnings("unchecked")
public JSONObject prepareReqJsonObj(String s1,String s2,String s3,String s4,String s5,String s6){
JSONObject jsonobj = new JSONObject();
jsonobj.put("name", s1);
jsonobj.put("emailid", s2);
jsonobj.put("mobile",s3 );
jsonobj.put("machineKey",s4 );
jsonobj.put("productkey", s5);
jsonobj.put("serialNo", s6);
return jsonobj;