[英]How does a Post request is made via a Java swing GUI
I have made a GUI in Java Swing and want to hit an API (POST method) On a button click like 我在Java Swing中创建了一个GUI,想要点击一个API(POST方法)点击按钮就像
http://www.myapi.domain/v1 HTTP://www.myapi.domain/v1
I have to send a JSON object as a request parameter along with the header ie (Headers["custom-Header"] == "AXYZ") I have converted my Strings to a JSON object like 我必须发送一个JSON对象作为请求参数以及标题即(Headers [“custom-Header”] ==“AXYZ”)我已将我的字符串转换为JSON对象,如
{"machineKey":"","serialNumber":"","name":"","mobile":"","productKey":"","email":""}
What I simply did is 我只是做了
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);
}
Now, how can I make request directly from my GUI button to Hit the API, what way I can achieve this and get a response. 现在,我如何直接从我的GUI按钮发出命令来命中API,我可以通过什么方式实现这一点并获得响应。
Thanks for your help in advance :) 在此先感谢您的帮助:)
Have a look at this example on how to use HttpClient to POST your data. 看看这个例子 ,了解如何使用HttpClient来发布数据。
Basically you create an HttpClient, create a POST request, fill that request with your data and then submit it. 基本上,您创建一个HttpClient,创建一个POST请求,用您的数据填充该请求,然后提交它。
Added this as a generic Utility class for all post requests 将其添加为所有发布请求的通用实用工具类
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();
}
}
}
}
and this one in Action listener -> 而这一个在Action侦听器中 - >
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.