我是android的新手,我不熟悉使用asynctask,我正在登录我的webservice,但是在尝试读取行上的输入流时失败
InputStream in = urlConnection.getInputStream();
这是我的代码:
package com.test.connector;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Authenticator;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import android.content.Context;
import android.os.AsyncTask;
public class TestConnection extends AsyncTask<String, String, String> {
static InputStream firstCertificate = null;
static InputStream secondCertificate = null;
static InputStream thirdCertificate = null;
private static String htmlString;
public void passCertificates(Context c){
c.getAssets();
try {
firstCertificate = c.getAssets().open("certificate1.crt");
} catch (IOException e) {
e.printStackTrace();
}
try {
secondCertificate=c.getAssets().open("certificate2.crt");
} catch (IOException e) {
e.printStackTrace();
}
try {
thirdCertificate=c.getAssets().open("certificate3");
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(String... params) {
String webHtmlData=null;
String inpUrl=params[0];
final String username=params[1];
final String password=params[2];
CertificateFactory cf = null;
try {
cf = CertificateFactory.getInstance("X.509");
} catch (CertificateException e) {
e.printStackTrace();
}
Certificate ca1 = null;
Certificate ca2 = null;
Certificate ca3 = null;
try {
ca1 = cf.generateCertificate(firstCertificate);
ca2 = cf.generateCertificate(secondCertificate);
ca3 = cf.generateCertificate(thirdCertificate);
} catch (CertificateException e) {
e.printStackTrace();
}
finally {
try {
firstCertificate.close();
secondCertificate.close();
thirdCertificate.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// Create a KeyStore containing our trusted CAs
String keyStoreType = KeyStore.getDefaultType();
KeyStore keyStore = null;
try {
keyStore = KeyStore.getInstance(keyStoreType);
} catch (KeyStoreException e) {
e.printStackTrace();
}
try {
keyStore.load(null, null);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
keyStore.setCertificateEntry("ca1", ca1);
} catch (KeyStoreException e) {
e.printStackTrace();
}
try {
keyStore.load(null, null);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
keyStore.setCertificateEntry("ca2", ca2);
} catch (KeyStoreException e) {
e.printStackTrace();
}
try {
keyStore.load(null, null);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
keyStore.setCertificateEntry("ca3", ca3);
} catch (KeyStoreException e) {
e.printStackTrace();
}
// Create a TrustManager that trusts the CAs in our KeyStore
String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = null;
try {
tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
try {
tmf.init(keyStore);
} catch (KeyStoreException e) {
e.printStackTrace();
}
// Create an SSLContext that uses our TrustManager
SSLContext context = null;
try {
context = SSLContext.getInstance("TLS");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
try {
context.init(null, tmf.getTrustManagers(), null);
} catch (KeyManagementException e) {
e.printStackTrace();
}
//authentication credentials
Authenticator myAuth = new Authenticator()
{
@Override
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password.toCharArray());
}
};
Authenticator.setDefault(myAuth);
// Tell the URLConnection to use a SocketFactory from our SSLContext
URL url = null;
try {
url = new URL(inpUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpsURLConnection urlConnection = null;
try {
urlConnection = (HttpsURLConnection)url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
urlConnection.setSSLSocketFactory(context.getSocketFactory());
try {
InputStream in = urlConnection.getInputStream();// my code fails here
webHtmlData=readStream(in);
} catch (IOException e) {
e.printStackTrace();
}
return webHtmlData;
}
public String readStream(InputStream in) {
StringBuilder response = null;
try {
BufferedReader is =
new BufferedReader(new InputStreamReader(in));
String inputLine;
response = new StringBuilder();
while ((inputLine = is.readLine()) != null) {
response.append(inputLine);
}
is.close();
}
catch (Exception e) {
e.printStackTrace();
}
return response.toString();
}
我正在通过mainActivity类使用以下命令执行doInBackground
TestConnection tc=new TestConnection;
tc.passCertificates(this);
String[] param[]={"example.com","username","password"}
tc.doInBackground(param);
但是相同的代码可以作为没有asyncTask的java应用程序。
这是我的Logcat
04-26 07:30:03.535: D/dalvikvm(1446): GC_FOR_ALLOC freed 37K, 4% free 2949K/3064K, paused 47ms, total 50ms
04-26 07:30:03.535: I/dalvikvm-heap(1446): Grow heap (frag case) to 3.558MB for 635812-byte allocation
04-26 07:30:03.585: D/dalvikvm(1446): GC_FOR_ALLOC freed 2K, 4% free 3567K/3688K, paused 40ms, total 40ms
04-26 07:30:03.935: W/System.err(1446): android.os.NetworkOnMainThreadException
04-26 07:30:03.945: W/System.err(1446): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
04-26 07:30:03.945: W/System.err(1446): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-26 07:30:03.955: W/System.err(1446): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-26 07:30:03.955: W/System.err(1446): at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-26 07:30:03.955: W/System.err(1446): at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
04-26 07:30:03.955: W/System.err(1446): at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
04-26 07:30:03.965: W/System.err(1446): at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
04-26 07:30:03.965: W/System.err(1446): at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
04-26 07:30:03.965: W/System.err(1446): at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
04-26 07:30:03.965: W/System.err(1446): at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
04-26 07:30:03.965: W/System.err(1446): at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
04-26 07:30:03.965: W/System.err(1446): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:296)
04-26 07:30:03.965: W/System.err(1446): at com.android.okhttp.internal.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:179)
04-26 07:30:03.965: W/System.err(1446): at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:246)
04-26 07:30:03.965: W/System.err(1446): at com.ovid.connector.Connector.downloadWebData(Connector.java:209)
04-26 07:30:03.965: W/System.err(1446): at com.ovid.connector.Connector.doInBackground(Connector.java:245)
04-26 07:30:03.965: W/System.err(1446): at com.ovid.connector.Connector.connectAndGetHtmlData(Connector.java:48)
04-26 07:30:03.965: W/System.err(1446): at com.ovid.expandablelistview.MainActivity.onCreate(MainActivity.java:70)
04-26 07:30:03.965: W/System.err(1446): at android.app.Activity.performCreate(Activity.java:5231)
04-26 07:30:03.965: W/System.err(1446): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-26 07:30:03.965: W/System.err(1446): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-26 07:30:03.965: W/System.err(1446): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-26 07:30:03.965: W/System.err(1446): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-26 07:30:03.965: W/System.err(1446): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-26 07:30:03.965: W/System.err(1446): at android.os.Handler.dispatchMessage(Handler.java:102)
04-26 07:30:03.965: W/System.err(1446): at android.os.Looper.loop(Looper.java:136)
04-26 07:30:03.965: W/System.err(1446): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-26 07:30:03.995: W/System.err(1446): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 07:30:03.995: W/System.err(1446): at java.lang.reflect.Method.invoke(Method.java:515)
04-26 07:30:03.995: W/System.err(1446): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-26 07:30:03.995: W/System.err(1446): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-26 07:30:03.995: W/System.err(1446): at dalvik.system.NativeStart.main(Native Method)
04-26 07:30:04.035: D/AndroidRuntime(1446): Shutting down VM
04-26 07:30:04.035: W/dalvikvm(1446): threadid=1: thread exiting with uncaught exception (group=0xb2ae0ba8)
04-26 07:30:04.055: E/AndroidRuntime(1446): FATAL EXCEPTION: main
04-26 07:30:04.055: E/AndroidRuntime(1446): Process: com.example.expandablelistview, PID: 1446
04-26 07:30:04.055: E/AndroidRuntime(1446): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.expandablelistview/com.ovid.expandablelistview.MainActivity}: java.lang.IllegalArgumentException: String input must not be null
04-26 07:30:04.055: E/AndroidRuntime(1446): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
04-26 07:30:04.055: E/AndroidRuntime(1446): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-26 07:30:04.055: E/AndroidRuntime(1446): at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-26 07:30:04.055: E/AndroidRuntime(1446): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-26 07:30:04.055: E/AndroidRuntime(1446): at android.os.Handler.dispatchMessage(Handler.java:102)
04-26 07:30:04.055: E/AndroidRuntime(1446): at android.os.Looper.loop(Looper.java:136)
04-26 07:30:04.055: E/AndroidRuntime(1446): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-26 07:30:04.055: E/AndroidRuntime(1446): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 07:30:04.055: E/AndroidRuntime(1446): at java.lang.reflect.Method.invoke(Method.java:515)
04-26 07:30:04.055: E/AndroidRuntime(1446): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-26 07:30:04.055: E/AndroidRuntime(1446): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-26 07:30:04.055: E/AndroidRuntime(1446): at dalvik.system.NativeStart.main(Native Method)
04-26 07:30:04.055: E/AndroidRuntime(1446): Caused by: java.lang.IllegalArgumentException: String input must not be null
04-26 07:30:04.055: E/AndroidRuntime(1446): at org.jsoup.helper.Validate.notNull(Validate.java:26)
04-26 07:30:04.055: E/AndroidRuntime(1446): at org.jsoup.parser.TreeBuilder.initialiseParse(TreeBuilder.java:24)
04-26 07:30:04.055: E/AndroidRuntime(1446): at org.jsoup.parser.TreeBuilder.parse(TreeBuilder.java:40)
04-26 07:30:04.055: E/AndroidRuntime(1446): at org.jsoup.parser.HtmlTreeBuilder.parse(HtmlTreeBuilder.java:54)
04-26 07:30:04.055: E/AndroidRuntime(1446): at org.jsoup.parser.Parser.parse(Parser.java:90)
04-26 07:30:04.055: E/AndroidRuntime(1446): at org.jsoup.Jsoup.parse(Jsoup.java:58)
04-26 07:30:04.055: E/AndroidRuntime(1446): at com.ovid.utils.ListProvider.getJournalName(ListProvider.java:15)
04-26 07:30:04.055: E/AndroidRuntime(1446): at com.ovid.expandablelistview.MainActivity.onCreate(MainActivity.java:73)
04-26 07:30:04.055: E/AndroidRuntime(1446): at android.app.Activity.performCreate(Activity.java:5231)
04-26 07:30:04.055: E/AndroidRuntime(1446): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-26 07:30:04.055: E/AndroidRuntime(1446): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
04-26 07:30:04.055: E/AndroidRuntime(1446): ... 11 more
答案 0 :(得分:1)
试试这个..
new TestConnection().execute();
答案 1 :(得分:0)
new TestConnection().execute(params);
并在延伸线中
AsyncTask<String[],Void,String>
答案 2 :(得分:0)
onPostExecute
的重载AsyncTask
方法。并致电tc.execute()
而不是tc.doInBackground()
:
@Override
protected void onPostExecute(Void result) {
//Move your code of reading inputStream here
}
execute()
调用doInBackground
并调用onPostEcecute
。