2011-07-19 4 views
2

Ce code fonctionne très bien sur les téléphones cellulaires SonyEricsson et Motorola, mais sur Nokia, il échoue au début, ne fait aucune demande ou renvoie une réponse vide, selon le modèle. Essayé différents codes avec des tampons d'octets, des flux, etc, le résultat est toujours le même. Quel est le problème?J2ME, Nokia, HttpConnection

Répondre

0

l'essayer

import java.io.ByteArrayOutputStream; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.util.Enumeration; 
import java.util.Hashtable; 
import javax.microedition.io.Connector; 
import javax.microedition.io.HttpConnection; 

public class Transport { 

public static int BUFFER_LENGTH = 100;//1024; 
public static boolean USE_FLUSH = false; 

static { 
    if (DeviceDetect.getDeivice() == DeviceDetect.SAMSUNG) { 
     BUFFER_LENGTH = 1024; 
     USE_FLUSH = true; 
    } 
} 
public static String SESSION_COOKIE = null; 


private static int rnd = 1; 

private static final String request(String url, String method, Hashtable params) throws IOException { 
    HttpConnection conn = null; 
    DataOutputStream dos = null; 
    InputStream in = null; 

    try { 

     String encodedParams = null; 
     if (params != null && params.size() > 0) { 
      encodedParams = getEncodedParams(params); 
     } 
     if (method == null) { 
      if (encodedParams.length() < 2000) { 
       method = "GET"; 
      } else { 
       method = "POST"; 
      } 
     } 

     method = method != null && method.equalsIgnoreCase("POST") ? HttpConnection.POST : HttpConnection.GET; 

     if (method.equalsIgnoreCase(HttpConnection.GET)) { 
      if (encodedParams != null) { 
       url += (url.indexOf("?") == -1 ? "?" : "&") + encodedParams; 
       encodedParams = null; 
      } 
      url += (url.indexOf("?") == -1 ? "?" : "&") + "_rnd=" + rnd++; 
     } 

     conn = (HttpConnection) Connector.open(url, Connector.READ_WRITE, true); 
     if (conn == null) { 
      throw new IOException("HttpConnection is null, please check Access Point configuration"); 
     } 

     conn.setRequestMethod(method); 
     conn.setRequestProperty("User-Agent", UserAgent.getUserAgent()); 
     conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
     if (SESSION_COOKIE != null) { 
      conn.setRequestProperty("Cookie", SESSION_COOKIE); 
     } 

     byte[] buff = new byte[BUFFER_LENGTH]; 
     if (encodedParams != null) { 
      byte[] bytes = encodedParams.getBytes("UTF-8"); 
      String lengthStr = bytes.length + ""; 
      conn.setRequestProperty("Content-Length", lengthStr); 

      dos = conn.openDataOutputStream(); 
      if (dos == null) { 
       throw new IOException("OutputStream is null, please check Access Point configuration"); 
      } 

      for (int i = 0, l = bytes.length; i < l; i += Transport.BUFFER_LENGTH) { 
       if (Transport.BUFFER_LENGTH == 1) { 
        dos.writeByte(bytes[i]); 
       } else { 
        int count = Math.min(Transport.BUFFER_LENGTH, l - i); 
        System.arraycopy(bytes, i, buff, 0, count); 
        dos.write(buff, 0, count); 
       } 
       if (Transport.USE_FLUSH) { 
        dos.flush(); 
       } 
      } 

      dos.flush(); 
      try { 
       dos.close(); 
      } catch (IOException ex) { 
      } 
     } 

     String setCookie = conn.getHeaderField("Set-Cookie"); 
     if (setCookie != null) { 
      int ind1 = setCookie.indexOf("JSESSIONID="); 
      if (ind1 > -1) { 
       int ind2 = setCookie.indexOf(";", ind1); 
       SESSION_COOKIE = ind2 == -1 ? setCookie.substring(ind1) : setCookie.substring(ind1, ind2 + 1); 
      } 
     } 

     in = conn.openInputStream(); 
     if (in == null) { 
      throw new IOException("InputStream is null, please check Access Point configuration"); 
     } 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     int n = in.read(buff); 
     while (n > -1) { 
      baos.write(buff, 0, n); 
      n = in.read(buff); 
     } 
     baos.flush(); 
     baos.close(); 
     String response = new String(baos.toByteArray(), "UTF-8"); 
     try { 
      in.close(); 
     } catch (Exception ex) { 
     } 
     try { 
      conn.close(); 
     } catch (Exception ex) { 
     } 

     return response; 
    } finally { 
     try { 
      dos.close(); 
     } catch (Exception ex) { 
     } 
     try { 
      in.close(); 
     } catch (Exception ex) { 
     } 
     try { 
      conn.close(); 
     } catch (Exception ex) { 
     } 
    } 

} 

public static String getEncodedParams(Hashtable params) throws IOException { 
    String str = ""; 
    Enumeration keys = params.keys(); 
    while (keys.hasMoreElements()) { 
     String name = (String) keys.nextElement(); 
     Object value = params.get(name); 
     if (value == null || name == null) { 
      continue; 
     }    
     str += "&" + URLEncoder.encode(name.toString(), "UTF-8") + "=" + URLEncoder.encode(value.toString(), "UTF-8"); 
    } 
    if (str.length() > 1) { 
     str = str.substring(1); 
    } 
    return str; 
} 
} 
0

Je ne suis pas sûr si cela fonctionnera pour vous, mais j'ai eu un problème similaire et a constaté que la spécification d'un numéro de port fait fonctionner (sur certains modèles Nokia plus). i.e. convertir:

http://www.mysite.com/my_page.html 

à:

http://mysite.com:80/my_page.html 
Questions connexes