2010-06-22 58 views
5

Tôi hiện đang cố gắng xác thực với máy chủ qua http Nhận cuộc gọi. Mã được cung cấp bên dưới hoạt động khi được biên soạn trong một dự án java. Trả lại mã thông báo chính xác cho chương trình. Tuy nhiên bất cứ khi nào tôi cố gắng triển khai cùng một mã trong Android, tôi không nhận được mã thông báo được trả lại qua lệnh gọi Nhận.Yêu cầu Https, xác thực trong Android

Trong Android tôi đang trả về inputLine trong một hàm, tuy nhiên inputLine luôn là một chuỗi rỗng.

System.out.println() in mã thông báo được trả về.

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

cảm ơn sự giúp đỡ của bạn !!!

+0

bạn có thể xem những gì đang xảy ra ở phía máy chủ? Truy cập nhật ký, mã trạng thái, v.v. –

+0

thật không may, máy chủ được lưu trữ bên ngoài – Mango

+0

Khi tôi chạy mã trong Android, có vẻ như luôn thất bại tại dòng "InputStream ins - con.getInputStream();" nó ném IOException. Tuy nhiên điều này không xảy ra khi tôi đang chạy mã như một ứng dụng java. – Mango

Trả lời

2

Có thể bạn đã không thêm Giấy phép Internet vào dự án AndroidManifest.xml của mình. Nếu vậy, hãy thêm dòng sau như một đứa trẻ của nút <manifest/>:

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

Tôi đang sử dụng POST và FormEntity để lấy dữ liệu từ máy chủ (ví dụ như xác thực), và tôi chưa bao giờ có vấn đề gì:

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()); 
[...] 

có thể bạn sẽ tìm thấy hữu ích này

+0

điều này có vẻ thực sự dễ hiểu đối với SSL, tôi rất ngạc nhiên khi không sử dụng SchemeRegistry để đăng ký lược đồ "https" như người này đã làm: http://stackoverflow.com/questions/2603691/android-httpclient-and-https/2603786 # 2603786 –

Các vấn đề liên quan