2010-06-22 16 views
5

Attualmente sto cercando di autenticare con un server tramite una chiamata http Get. Il codice fornito di seguito funziona quando compilato in un progetto java. Restituire il token corretto al programma. Tuttavia, ogni volta che provo a implementare lo stesso codice in Android, non ottengo un token restituito tramite la chiamata Get.Richiesta Https, autenticazione su Android

In Android sto restituendo inputLine in una funzione, tuttavia inputLine è sempre una stringa vuota.

Il file system.out.println() stampa il token restituito.

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.URL; 
import javax.net.ssl.HttpsURLConnection; 

public class JavaHttpsExample 
{ 

public static void main(String[] args) 
{ String inputLine = new String(); 
    try 
    { 
    String httpsURL = "https://the url"; 
    URL myurl = new URL(httpsURL); 
    HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection(); 
    InputStream ins = con.getInputStream(); 
    InputStreamReader isr=new InputStreamReader(ins); 
    BufferedReader in =new BufferedReader(isr); 

    inputLine = in.readLine(); 

    System.out.println(inputLine); 

    in.close(); 


    } 
    catch(IOException e) 
    { 
     e.printStackTrace(); 
    } 
} 
} 

grazie per il vostro aiuto !!!

+0

Riesci a vedere cosa sta succedendo sul lato server? Log di accesso, codici di stato ecc. –

+0

sfortunatamente no, il server è ospitato al di fuori – Mango

+0

Quando eseguo il codice in Android, sembra sempre fallire sulla linea "InputStream ins - con.getInputStream();" getta IOException. Tuttavia ciò non accade quando eseguo il codice come applicazione java. – Mango

risposta

2

Probabilmente non hai aggiunto l'autorizzazione Internet ai tuoi progetti AndroidManifest.xml. Se è così, aggiungere la seguente riga come un figlio del nodo <manifest/>:

<uses-permission android:name="android.permission.INTERNET" /> 
2

sto usando POST e FormEntity per il recupero dei dati dal server (come l'autenticazione), e non ho mai avuto problemi:

final String httpsURL = "https://the url"; 
final DefaultHttpClient client = new DefaultHttpClient(); 
final HttpPost httppost = new HttpPost(httpsURL); 

//authentication block: 
final List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>(); 
nvps.add(new BasicNameValuePair("userName", userName)); 
nvps.add(new BasicNameValuePair("password", password)); 
final UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps, HTTP.UTF_8); 
httppost.setEntity(p_entity); 

//sending the request and retrieving the response: 
HttpResponse response = client.execute(httppost); 
HttpEntity responseEntity = response.getEntity(); 

//handling the response: responseEntity.getContent() is your InputStream 
final InputSource inputSource = new InputSource(responseEntity.getContent()); 
[...] 

forse troverete questo utile

+0

questo sembra davvero semplice per SSL, sono sorpreso che non ci sia alcun uso di SchemeRegistry per registrare lo schema "https" come ha fatto questa persona: http://stackoverflow.com/questions/2603691/android-httpclient-and-https/2603786 # 2603786 –