1 public class CallAPI extends AsyncTask<String, String, String> {
2
3 public CallAPI(){
4 //set context variables if required
5 }
6
7 @Override
8 protected void onPreExecute() {
9 super.onPreExecute();
10 }
11
12 @Override
13 protected String doInBackground(String... params) {
14 String urlString = params[0]; // URL to call
15 String data = params[1]; //data to post
16 OutputStream out = null;
17
18 try {
19 URL url = new URL(urlString);
20 HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
21 out = new BufferedOutputStream(urlConnection.getOutputStream());
22
23 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
24 writer.write(data);
25 writer.flush();
26 writer.close();
27 out.close();
28
29 urlConnection.connect();
30 } catch (Exception e) {
31 System.out.println(e.getMessage());
32 }
33 }
34 }