2010-10-15 58 views
1

「基本YWRtaW46YW RtaW4 =」 是我的地址的正確代碼。我檢查j2me項目。認證問題.. !!使用HTTPURLCONNECTION的ANDROID

And on android getbase64 method returns「Basic YWRtaW46YW RtaW4 =」its true。

和ı使用它:

httpConnection.setRequestProperty( 「授權」,getBase64Encode());

最後性反應的代碼是401

任何想法???

protected void connect() { 
    InputStream is = null; 
    OutputStream os = null; 
    try { 

    url = new URL(getUrl()); 
    System.out.println(getUrl());// duzelt 
    queryString = encodeURL(queryString); 
    byte postmsg[] = queryString.getBytes("UTF-8"); 
    conn = url.openConnection(); 
    if (!(conn instanceof HttpURLConnection)) 
    throw new IOException("Not an HTTP connection"); 

    httpConnection = (HttpURLConnection) conn; 
    HttpOptions options=new HttpOptions(); 
    httpConnection.setDoInput(true); 
    httpConnection.setDoOutput(true); 
    httpConnection.setRequestMethod("GET"); 
    httpConnection.setRequestProperty("Authorization", 
    getBase64Encode()); 
    httpConnection.connect(); 
    os = httpConnection.getOutputStream();// ///////////////////baglantının 
    System.out.println("response code: "+ httpConnection.getResponseCode()); 
    // connect olup olmadıgını 
    // kontrol et 

    for (int i = 0; i < postmsg.length; i++) { 
    os.write(postmsg[i]); 
    } 
    if (!cancel) { 
    onReturn(httpConnection.getResponseCode(), httpConnection 
     .getInputStream()); 
    } 
    os.close(); 
    // httpConnection.close(); 
    } catch (Exception e) { 
    System.out.println(e.getMessage()); 
    try { 
    httpConnection.disconnect(); 
    Thread.sleep(60); 
    // cancel=true; eklenmesı gerekebilir 
    } catch (Exception ie) { 
    } 
    onError(e); 
    } 

回答

1

在HttpURLConnection上有一個setConnectTimeout(int)方法。

設置在建立到此URLConnection 實例指出的 資源的連接毫秒 的超時值。如果在此時建立的連接不可能爲 ,則SocketTimeoutException爲 。默認值爲0 ,表示無限超時。

httpConnection.setConnectTimeout(10000); /* connection timeout set to 10s */ 
+0

是的,我嘗試過,但沒有任何區別.. :(我可以告訴MJPEG文件,但我不能把鏈接:((我試圖與HTTPGET-defaulthttpclıent連接後,它的工作原理是什麼。 – atasoyh 2010-10-16 12:10:25

+0

不同的實現會導致不同的行爲:)爲了處理服務器接受連接但不迴應請求的情況,可能需要設置連接超時和讀取超時(setReadTimeout)。 – Jcs 2010-10-18 16:56:52

+1

I終於用了httpclient,沒問題.. – atasoyh 2010-10-21 14:46:35