2016-04-04 17 views
0

içinde params ile dönüş tipi arasında geçiş Merhaba, bu yüzden benim android uygulaması için tüm url isteğini işleyecek bir httpclient üzerinde çalışıyorum.Ne yapmak istediğimi emin aramak mümkün yöntemini çağırmak istiyorum. Şu anda tüm bu basit httpUrlconnection diyor. Bu sınıfta, onu neye çağırdığına göre aramak istediğim 3 tane başka metodum var. Bu herhangi bir Yardım büyük olacak: Yani buradaNasıl doInBackground yöntemi android

Ben yöntemini çağırarak am nasıl:

Ben bir web sitesine giriş için kullanmak istiyorsanız olduğunu
MyHttpClient task = new MyHttpClient(username, password); 

. Ben sadece bir şifre ile kullanmak istediğinizde Bunları yolları çalışması bu

MyHttpClient task = new MyHttpClient(); 

gibi diyoruz ama diğer üç yöntem çağırmak için nasıl bilmiyorum. İşte benim yaptığım sınıfım;

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(); 
} 
+1

tam olarak ne istiyorsunuz? –

+0

DoInBackground'unu değiştirmek istediğim, 4'ün istediğim yöntemini çağırmak istiyorum. Şu anda tüm bu yöntem httpDownloadData (url); – MNM

+0

AsyncTasks kullanmayın ve her şey iyi olacak. – Dogcat

cevap

1

Çağrı

AsyncTask # Birincisi gerekli yöntem temsil edeceğini ve ikinci URL'sini ayakta olurdu iki parametreleri içeren

(...) yürütün. Ya da bu iki param tek bir sınıfa sarın. Veya hiç kimse bu günlerde http için AsyncTask kullandığı için android-async-http, Retrofit veya Ion'a geçin.

+0

Teşekkür ederiz Dogcart Okhttp'nin yanı sıra Ion'a da bakıyorum, anlatabildiğim kadar güzel görünüyorlar. – MNM

0

Bunu daha iyi bir şekilde gerçekleştirebilirsiniz. Her arama için ayrı bir asenkronizasyon yazmalısınız (farklı URL). Parametreleri yapıcı veya Asynctask'ın String parametreleri aracılığıyla yönetin. Gerekirse, gerekli değişiklikleri yapabilmeniz için sınıfı özet olarak yapın. ,

... extends AsyncTask<String, Void, String>

String yana

ve Bitmap hem Object bir alt sınıfıdır ... extends AsyncTask<String, Void, Object>

için:

+1

> Bunu daha iyi bir şekilde elde edebilirsiniz. Bu nasıl optimize edilir? – Dogcat

1

Sonuçlar istediğiniz farklı iade adresinin sınıf tanımını değiştirmek isterseniz geçerli olacak. Tabii ki, 'u da Object'a döndürmek için hala doInBackground(...)'u değiştirmeniz gerekiyor.

+0

Farklı yöntemler kullanmak için doInBackgournd yöntemini değiştirmeye nasıl giderim? Parazitlere dayanarak mı? – MNM

+0

Peki hangisini istediğinizi nasıl tespit edersiniz? –

+0

Böyle bir şey denedim ve işe yaradığı görüldü – MNM

İlgili konular