Java HTTPS服务器连接
我决定使用StripeAPI进行支付模拟。我做了一些事情。但我在连接到服务器时遇到了一些问题。首先,我添加了一些信用卡信息,然后我将其发送到服务器,并从服务器发送到我在Stripe网站上的帐户。对于后端,我使用NodeJS。为了连接到服务器,我从谷歌上读到了这篇文章。顺便说一下,我为HTTPS做了这个认证。但我在后台处理函数时遇到了一些问题。Android监视器向我显示Java HTTPS服务器连接,java,android,https,server,nullpointerexception,Java,Android,Https,Server,Nullpointerexception,我决定使用StripeAPI进行支付模拟。我做了一些事情。但我在连接到服务器时遇到了一些问题。首先,我添加了一些信用卡信息,然后我将其发送到服务器,并从服务器发送到我在Stripe网站上的帐户。对于后端,我使用NodeJS。为了连接到服务器,我从谷歌上读到了这篇文章。顺便说一下,我为HTTPS做了这个认证。但我在后台处理函数时遇到了一些问题。Android监视器向我显示doInBackground(DataBaseTask.java:211)-它显示这一行InputStream caInput=
doInBackground(DataBaseTask.java:211)
-它显示这一行InputStream caInput=new BufferedInputStream(contexting.getAssets().open(“cert.pem”)代码>和另一行代码是类示例扩展AsyncTask
。请帮帮我
这是我的代码:
public class DataBaseTask extends Activity{
String command = "";
Token token;
public DataBaseTask(String mCommand, Token mToken) {
command = mCommand;
token = mToken;
Log.e("Helloooo", "Helloooo DATABASETASK");
new Example().execute();
}
class Example extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.e("AsyncTask", "onPreExecute");
}
@Override
protected String doInBackground(String... params) {
final Context contexting = DataBaseTask.this;
Log.e("Context", contexting.toString());
Log.e("Stop here", "Stop here");
String echoData = "";
if (command.equals("SANDTOKEN")) {
try {
// Load CAs from an InputStream
// (could be from a resource or ByteArrayInputStream or ...)
CertificateFactory cf = CertificateFactory.getInstance("X.509");
// String fileName = "Download/cert.pem";
// String path = Environment.getExternalStorageDirectory()+"/"+fileName;
// File file = new File(path);
// FileInputStream fileInputStream = new FileInputStream(file);
InputStream caInput = new BufferedInputStream(contexting.getAssets().open("cert.pem"));
Certificate ca = cf.generateCertificate(caInput);
System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
Log.e("ca", "ca= " + ((X509Certificate) ca).getSubjectDN());
// Create a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
keyStore.load(null, null);
keyStore.setCertificateEntry("ca", ca);
// Create a TrustManager that trusts the CAs in our KeyStore
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
tmf.init(keyStore);
// Create an SSLContext that uses our TrustManager
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);
// Tell the URLConnection to use a SocketFactory from our SSLContext
//URL url = new URL(urlString);
URL url = new URL("https://Address:4567/charge");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setSSLSocketFactory(context.getSocketFactory());
StringBuilder builder = new StringBuilder();
builder.append(URLEncoder.encode("stripeToken", "UTF-8"));
builder.append("=");
builder.append(URLEncoder.encode(token.getId(), "UTF-8"));
String urlParameters = builder.toString();
Log.e("String param ", urlParameters);
connection.setRequestMethod("POST");
connection.setDoOutput(true);
DataOutputStream dStream = new DataOutputStream(connection.getOutputStream());
dStream.writeBytes(urlParameters);
dStream.flush();
dStream.close();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line = "";
StringBuilder responseOutput = new StringBuilder();
while ((line = br.readLine()) != null) {
Log.e("DatabaseTask", line);
responseOutput.append(line);
}
br.close();
echoData = responseOutput.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
return echoData;
}
@Override
protected void onPostExecute(String mData) {
Log.e("DatabaseTask", "onPostExecute result: " + mData);
}
}
}
为类“DataBaseTask”创建一个新文件,该类扩展异步任务
(而不是扩展活动
):
现在,您可以从任何地方调用新的“DataBaseTask”。这里有一个使用onClick
事件调用新的“DataBaseTask”AsyncTask
的示例
public void onClickDataBaseAsyncTask(View view){
try{
DataBaseTaskListener listener = new DataBaseTaskListener() {
@Override
public void onCompletedSendData(String result) {
//Do what you need with the data
}
};
DataBaseTask c = new DataBaseTask(YourCallingActivity.this, yourCommand, yourToken, listener);
c.execute();
}
catch (Exception ex){
Log.e(TAG, ex.getMessage());
}
}
@但是我写了final Context contexting=DataBaseTask.this代码>又一个。。。不,您不能自己使用运算符new
初始化活动派生类。很抱歉,您忽略了它。但是,DataBaseTask
类的作用是什么(而不仅仅是AsyncTask
)。它似乎没有与之关联的UI,但没有用于创建UI的onCreate
方法。请更清楚地说明问题,并解释调用DataBaseTask
的位置以及您的意图。@Barns我有另一个类,我这样调用它DataBaseTask db=newdatabasetask(“SANDTOKEN”,token)代码>这就是all@Barns当这个类DataBaseTask
正常工作时,它会将信息发送到服务器,我会从服务器的仪表板(stripe的网站)上查看这些信息
public class DataBaseTask extends AsyncTask<String, Void, String> {
private static final String TAG = "DataBaseTask";
// Just in case you want a ProgressDialog uncomment this and the associated code
//private ProgressDialog pDialog;
private DataBaseTaskListener mListener;
Context mContext;
String mCommand = "";
Token mToken;
public DataBaseTask(Context context, String command, Token token, DataBaseTaskListener listener){
this.mContext = context;
this.mCommand = command;
this.mToken = token;
this.mListener = listener;
}
@Override
protected String doInBackground(String... params) {
String result = "";
try{
// Put your code here!!
}
catch(Exception ex){
Log.e(TAG, ex.getMessage());
}
return result;
}
@Override
protected void onPostExecute(String result) {
try{
// In case you want a ProgressDialog
//pDialog.dismiss();
// Trigger the listener for the call back sending the result
mListener.onCompletedSendData(result);
Log.e(TAG, "onPostExecute result: " + mData);
}
catch(Exception ex){
Log.e(TAG, ex.getMessage());
}
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// In case you want a ProgressDialog
//pDialog = new ProgressDialog(context);
//pDialog.setMessage("Sending Data...");
//pDialog.setIndeterminate(false);
//pDialog.setCancelable(false);
//pDialog.show();
}
}
public interface DataBaseTaskListener {
void onCompletedSendData(String result);
}
public void onClickDataBaseAsyncTask(View view){
try{
DataBaseTaskListener listener = new DataBaseTaskListener() {
@Override
public void onCompletedSendData(String result) {
//Do what you need with the data
}
};
DataBaseTask c = new DataBaseTask(YourCallingActivity.this, yourCommand, yourToken, listener);
c.execute();
}
catch (Exception ex){
Log.e(TAG, ex.getMessage());
}
}