Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 1E589200CB0 for ; Fri, 23 Jun 2017 15:02:05 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 1CBD2160BD4; Fri, 23 Jun 2017 13:02:05 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 3B7BD160BCA for ; Fri, 23 Jun 2017 15:02:04 +0200 (CEST) Received: (qmail 35717 invoked by uid 500); 23 Jun 2017 13:02:03 -0000 Mailing-List: contact issues-help@cordova.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Delivered-To: mailing list issues@cordova.apache.org Received: (qmail 35706 invoked by uid 99); 23 Jun 2017 13:02:03 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 23 Jun 2017 13:02:03 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id 01AF8C1431 for ; Fri, 23 Jun 2017 13:02:03 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -99.202 X-Spam-Level: X-Spam-Status: No, score=-99.202 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, RP_MATCHES_RCVD=-0.001, SPF_PASS=-0.001, USER_IN_WHITELIST=-100] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id 90d0tI0IRbR2 for ; Fri, 23 Jun 2017 13:02:01 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTP id CF4C05F3F0 for ; Fri, 23 Jun 2017 13:02:00 +0000 (UTC) Received: from jira-lw-us.apache.org (unknown [207.244.88.139]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id 4C220E0069 for ; Fri, 23 Jun 2017 13:02:00 +0000 (UTC) Received: from jira-lw-us.apache.org (localhost [127.0.0.1]) by jira-lw-us.apache.org (ASF Mail Server at jira-lw-us.apache.org) with ESMTP id 040B92193E for ; Fri, 23 Jun 2017 13:02:00 +0000 (UTC) Date: Fri, 23 Jun 2017 13:02:00 +0000 (UTC) From: "Anoop Varghese (JIRA)" To: issues@cordova.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Created] (CB-12946) External API call from custom cordova plugin for android MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 archived-at: Fri, 23 Jun 2017 13:02:05 -0000 Anoop Varghese created CB-12946: ----------------------------------- Summary: External API call from custom cordova plugin for android Key: CB-12946 URL: https://issues.apache.org/jira/browse/CB-12946 Project: Apache Cordova Issue Type: Task Environment: Android Reporter: Anoop Varghese Priority: Critical I am creating a custom cordova plugin and want to make an External API call inside it. But the Native Java method is called from plugin Js when the API call is made inside it. The code which i have used is given below. +**cordova-plugin-tsp.js**+ {code:js} cordova.define("cordova-plugin-tsp.TSP", function(require, exports, module) { //var exec = require('cordova/exec'); var TSP = function() { var _appID = ""; }; TSP.prototype.startInit = function(appId) { TSP._appID = appId; return this; }; TSP.prototype.tsp_a10000 = function(arg0, success, error) { cordova.exec(success, error, "tsp", "tsp_a10000", [arg0]); }; //------------------------------------------------------------------- if(!window.plugins) window.plugins = {}; if (!window.plugins.TSP) window.plugins.TSP = new TSP(); if (typeof module != 'undefined' && module.exports) module.exports = TSP; }); {code} *+tsp.java+* {code:java} import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.util.Log; import org.apache.cordova.api.CordovaPlugin; import org.apache.cordova.api.CallbackContext; import org.apache.cordova.api.CordovaInterface; import org.apache.cordova.api.PluginResult; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import sample.test; /** * This class echoes a string called from JavaScript. */ public class tsp extends CordovaPlugin { // This is to prevent an issue where if two Javascript calls are made to OneSignal expecting a callback then only one would fire. private static void callbackSuccess(CallbackContext callbackContext, JSONObject jsonObject) { if (jsonObject == null) // in case there are no data jsonObject = new JSONObject(); PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, jsonObject); pluginResult.setKeepCallback(true); callbackContext.sendPluginResult(pluginResult); } private static void callbackError(CallbackContext callbackContext, JSONObject jsonObject) { if (jsonObject == null) // in case there are no data jsonObject = new JSONObject(); PluginResult pluginResult = new PluginResult(PluginResult.Status.ERROR, jsonObject); pluginResult.setKeepCallback(true); callbackContext.sendPluginResult(pluginResult); } private static void callbackError(CallbackContext callbackContext, String str) { PluginResult pluginResult = new PluginResult(PluginResult.Status.ERROR, str); pluginResult.setKeepCallback(true); callbackContext.sendPluginResult(pluginResult); } @Override public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { if (action.equals("tsp_a10000")) { String message = args.getString(0); this.tsp_a10000(message, callbackContext); return true; } return false; } private void tsp_a10000(String message, CallbackContext callbackContext) { JSONObject jsonIds = new JSONObject(); // test s=new test(); if (message != null && message.length() > 0) { try { String s=""; s += "\n OS Version: " + System.getProperty("os.version") + "(" + android.os.Build.VERSION.INCREMENTAL + ")"; s += "\n OS API Level: " + android.os.Build.VERSION.SDK_INT; s += "\n Device: " + android.os.Build.DEVICE; s += "\n Model (and Product): " + android.os.Build.MODEL + " ("+ android.os.Build.PRODUCT + ")"; jsonIds.put("message", message); jsonIds.put("Debug-infos",s); try { HttpGet httpget = new HttpGet("http://echo.jsontest.com/title/ipsum/content/blah"); CloseableHttpResponse response = HttpClients.createDefault().execute(httpget); int responseCode = response.getStatusLine().getStatusCode(); jsonIds.put("GET_request_Url",httpget.getURI().toString()); jsonIds.put("Response_Code", Integer.toString(responseCode)); BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer sb = new StringBuffer(""); String line = ""; while ((line = in.readLine()) != null) { sb.append("\n"+line); } in.close(); jsonIds.put("Content",line); } catch (Exception e) { jsonIds.put("Exception", e.toString()); callbackError(callbackContext,jsonIds); //e.printStackTrace(); } callbackSuccess(callbackContext,jsonIds); }catch(Throwable t){ t.printStackTrace(); } } else { callbackError(callbackContext,"Expected one non-empty string argument."); } } } {code} *+plugin.xml+* {code:XML} cordova-plugin-tsp {code} *+The plugin method call i made in my App+* {code:js} window['plugins'].TSP.tsp_a10000('Hi',function(data){ console.log('tsp_a10000 sucess: ' + JSON.stringify(data)); },function(error){ console.log('tsp_a10000 error: ' + JSON.stringify(error)); }); {code} Please help me... -- This message was sent by Atlassian JIRA (v6.4.14#64029) --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscribe@cordova.apache.org For additional commands, e-mail: issues-help@cordova.apache.org