I have made a GUI in Java Swing and want to hit an API (POST method) On a button click like
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
{"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.
Thanks for your help in advance :)
Have a look at this example on how to use HttpClient to POST your data.
Basically you create an HttpClient, create a POST request, fill that request with your data and then submit it.
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 ->
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.