Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package bi; import SMSgateway.SMS; import SMSgateway.SMS.Responce; import SMSgateway.SMSHolder; import SMSgateway.discont.DiscontDocBuilder; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import com.prim.core.AbstractApplication; import com.prim.support.URLRequester; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.lang.reflect.Type; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import org.json.JSONObject; /** * * @author bezdatiuzer */ public class sms_delivery extends PrimService { public sms_delivery(AbstractApplication app) throws Exception { super("sms_delivery", app); } public String sendSms(SMSHolder sh) throws Exception { Responce res; /*MobakDocBiulder mdb = MobakDocBiulder.getInstance("NerudTorg", "4Ro21sPc"); Document doc = mdb.createDocument(sh); URLRequester urlreq = new URLRequester(MobakDocBiulder.URL, "utf8"); urlreq.setPostMethod(); urlreq.addPostParameter("xml", primXml.documentToString(doc)); urlreq.create();*/ DiscontDocBuilder ddb = DiscontDocBuilder.getInstance("330krasn0907", "134087"); JSONObject requestBody = ddb.createDiscontJSONRequest(sh); URL url = new URL(DiscontDocBuilder.URL); HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("POST"); con.setDoOutput(true); con.setDoInput(true); con.setRequestProperty("Content-Type", "application/json"); con.setRequestProperty("Accept", "application/json"); OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream(), "UTF-8"); wr.write(requestBody.toString()); wr.flush(); String str; StringBuilder sb = new StringBuilder(); int HttpResult = con.getResponseCode(); if (HttpResult == HttpURLConnection.HTTP_OK) { BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8")); String line = null; while ((line = br.readLine()) != null) { sb.append(line + "\n"); } br.close(); //System.out.println("" + sb.toString()); str = sb.toString(); } else { //System.out.println(con.getResponseMessage()); addError(con.getResponseMessage()); str = con.getResponseMessage(); } URLRequester urlreq = new URLRequester(DiscontDocBuilder.URL, "utf8"); //urlreq.setPostMethod(); GsonBuilder gb = new GsonBuilder(); Gson gson = gb.create(); //map.put("mes", "330krasn0907"); //String body="{\"login\":\"330krasn0907\",\"password\":\"134087\",\"clientId\":\"1234\",\"phone\":\"+79124850319\",\"text\":\"test\"}"; //urlreq.addPostParameter("login", "330krasn0907"); //urlreq.addPostParameter("password", "134087"); //urlreq.addPostParameter("messages", requestBody); //urlreq.setUrlEncoding("UTF-8"); //urlreq.addPostParameter("json",gson.toJson(map)); //urlreq.create(); String resp = urlreq.getResult(); Type maptype = new TypeToken<HashMap<String, String>>() { }.getType(); HashMap<String, String> urlres = gson.fromJson(resp, maptype); /*String str; if (urlreq.getErrors().isEmpty() && !urlres.get("status").equals("error")) { //str = "success " + urlreq.getResult(); str = " ? "; //str+=" "+requestBody.toString()+"; res="+urlreq.getResult()+";"; log(sh); } else { str = "error " + urlreq.getErrors().toString() + "; url_responce_description: " + urlres.get("description") + "; req_text=" + requestBody.toString() + "; res="; for (String s : urlres.keySet()) { str += s + " - " + urlres.get(s) + "; "; } }*/ return str; } private void log(SMSHolder sh) throws Exception { bi.sms_queue queueServ = new sms_queue(getApp()); for (SMS sms : sh.getSMSList()) { queueServ.addSms(sms.number, sms.txt, sms.sendEntityId, sms.aimEntityId, true); if (!checkAr(queueServ.getActionResult())) { return; } } } }