嗨,我正在开发一个http客户端,该客户端将处理我的android应用程序的所有url请求。我想要做的是能够调用我想要调用的特定方法。现在它所做的只是调用基本的httpUrlconnection。在这个类中,我有3个其他方法,我想根据调用它的内容来调用它们。这方面的任何帮助都将是伟大的:
以下是我如何调用方法:
MyHttpClient task = new MyHttpClient(username, password);
如果我想用它登录网站的话。当我只想在没有密码的情况下使用它时,我会这样称呼它
MyHttpClient task = new MyHttpClient();
这些方法都有效,但我不知道如何调用其他三种方法。这是我做的课;
public class MyHttpClient extends AsyncTask<String, Void, String> {
private String username;
private String password;
//this is used if you need a password and username
//mainly for logins to a webserver
public MyHttpClient(String username, String password)
{
this.username = username;
this.password = password;
}
//used for image downloading
public MyHttpClient(){}
@Override
protected String doInBackground(String... params) {
String url = params[0];
return httpDownloadData(url);
}
//this is used for a simple http download of json files or xml file
//it must return a string fom the http request
private String httpDownloadData(String myUrl)
{
String respone = null;
HttpURLConnection urlConnection = null;
try
{
URL url = new URL(myUrl);
//put in the username and pssword for parmas to send to url
//this is good for login
if (username!=null)
{
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password.toCharArray());
}
});
}
urlConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
if(inputStream != null)
{
respone = streamToString(inputStream);
inputStream.close();
}
}catch (IOException ie)
{
Log.d("IOExceptrion:", "In http downloader");
ie.printStackTrace();
}finally {
if(urlConnection != null)
{
urlConnection.disconnect();
}
}
return respone;
}
//this is to download images from HTTP connections
private Bitmap httpBitmapDownloader(String myUrl)
{
HttpURLConnection urlConnection = null;
try {
URL url = new URL(myUrl);
urlConnection = (HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod("GET");
int statusCode = urlConnection.getResponseCode();
if (statusCode != 200)
{
return null;
}
InputStream inputStream = urlConnection.getInputStream();
if(inputStream != null)
{
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (urlConnection != null)
{
urlConnection.disconnect();
}
}
return null;
}
//download strings via https
//Todo Add the certificate handler so turst server
// TODO: 4/4/16 BKS needed for this
private String httpsDownloadData(String myUrl)
{
String respone = null;
HttpsURLConnection urlConnection = null;
//get the cert handler
try
{
KeyStore keyStore = KeyStore.getInstance("");
String algorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm);
tmf.init(keyStore);
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);
URL url = new URL(myUrl);
if (username!=null)
{
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password.toCharArray());
}
});
}
urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
//urlConnection.setSSLSocketFactory(context.getSocketFactory());
int statusCode = urlConnection.getResponseCode();
Log.d("Status code: " , Integer.toString(statusCode));
InputStream inputStream = urlConnection.getInputStream();
if(inputStream != null)
{
respone = streamToString(inputStream);
inputStream.close();
}
}catch (IOException e)
{
Log.d("downloading data: " , "in https webape");
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
Log.d("Trustmanager issue:" , "Error");
e.printStackTrace();
} catch (KeyStoreException e) {
Log.d("Keystore issues:", "Key needs att");
e.printStackTrace();
} catch (KeyManagementException e) {
Log.d("Key management:" , "Key issue");
e.printStackTrace();
}
return respone;
}
private Bitmap httpsBitmapDownloader(String myUrl)
{
HttpsURLConnection urlConnection = null;
try
{
KeyStore keyStore = KeyStore.getInstance("");
String algorithm = TrustManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmf = TrustManagerFactory.getInstance(algorithm);
tmf.init(keyStore);
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, tmf.getTrustManagers(), null);
URL url = new URL(myUrl);
urlConnection = (HttpsURLConnection)url.openConnection();
urlConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
int statusCode = urlConnection.getResponseCode();
if(statusCode != 200){return null;}
InputStream inputStream = urlConnection.getInputStream();
if(inputStream != null)
{
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
Log.d("bitmap download: " , "Https issues");
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} finally {
if (urlConnection != null)
{
urlConnection.disconnect();
}
}
return null;
}
//this is used for downloading strings from an http or https connection
private String streamToString(InputStream is) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
//add a fake parent to the line json data
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
return sb.toString();
}
如果您想返回不同类型的结果,请从修改您的类定义
... extends AsyncTask<String, Void, String>
至
... extends AsyncTask<String, Void, Object>
由于String
和Bitmap
都是Object
的子类,因此它将是有效的。当然,您仍然需要修改您的doInBackground(...)
以返回Object
。
调用
异步任务#执行(…)
有两个参数,其中第一个表示必要的方法,第二个表示其URL。或者将这两个param封装在一个类中。或者切换到android async http、Reform或Ion,因为现在没有人使用AsyncTask进行http。
您可以通过更优化的方式实现这一点。您应该为每个调用(不同的url)编写一个单独的异步任务。通过构造函数或asynctask的String参数管理参数。如有必要,将类设为抽象类,以便进行必要的更改。