2012-03-22 4 views
0

j'ai quelques problèmes quand j'utilisé ce code dans Android 4.0 je n'ai pas de réponse, mais j'ai reçu une réponse dans la version 2.2,2.3,2.3.3Comment obtenir réponse De HttpClient dans Android 4.0

 List<NameValuePair> pwadd = new ArrayList<NameValuePair>(); 
    pwadd.add(new BasicNameValuePair("UName", UName)); 

     HttpParams httpParameters = new BasicHttpParams(); 
     HttpClient httpclient = new DefaultHttpClient(httpParameters); 
     HttpPost httppost = new HttpPost(
       "http://10.0.2.2/new/webser/Load.php"); 
     httppost.setEntity(new UrlEncodedFormEntity(pwadd)); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 


      BufferedReader reader = new BufferedReader(
        new InputStreamReader(is, "iso-8859-1"), 8); 
      sb = new StringBuilder(); 
      sb.append(reader.readLine() + "\n"); 
      String line = "0"; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      String response = sb.toString(); 

ce devrais-je faire dans ce code pour fonctionner dans la version Android 4.0?

+2

Quel est le problème que vous obtenez? –

+0

sa réponse est nulle dans 4.0 mais dans 2.2.2.3 et 2.3.3 j'ai eu la réponse – user1153176

Répondre

4

essayez-les dans une tâche asynchrone. vous devez faire des choses de réseau dans un thread séparé de toute façon

public static String PrepareSendPostData_DetailsActivity(String station_id) { 

      //Create a new HttpClient and Post Header 
    HttpClient httpclient = new DefaultHttpClient(); 


    HttpPost httppost = new HttpPost("your url here"); 

    try { 

     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(100); 

     nameValuePairs.add(new BasicNameValuePair("param_1", "value_1")); 
     nameValuePairs.add(new BasicNameValuePair("param_2", "ok")); 
     nameValuePairs.add(new BasicNameValuePair("module", "dbgestion")); 
     nameValuePairs.add(new BasicNameValuePair("pdv_id", station_id)); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     // Execute HTTP Post Request 
     HttpResponse response = httpclient.execute(httppost); 

     String responseBody = getResponseBody(response); 

     if (responseBody != null) 
      return responseBody; 
     else 
      return null; 

    } catch (ClientProtocolException e) { 
     Log.e("exception here", e.getMessage().toString()); 
     return null; 
    } catch (IOException e) { 
     Log.e("exception here 2", e.getMessage().toString()); 
     return null; 
    } 

} 

public static String getResponseBody(HttpResponse response) { 

    String response_text = null; 
    HttpEntity entity = null; 
    try { 
     entity = response.getEntity(); 
     response_text = _getResponseBody(entity); 
    } catch (ParseException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     if (entity != null) { 
      try { 
       entity.consumeContent(); 
      } catch (IOException e1) { 
      } 
     } 
    } 
    return response_text; 
} 

public static String _getResponseBody(final HttpEntity entity) throws IOException, ParseException { 

    if (entity == null) { 
     throw new IllegalArgumentException("HTTP entity may not be null"); 
    } 

    InputStream instream = entity.getContent(); 

    if (instream == null) { 
     return ""; 
    } 

    if (entity.getContentLength() > Integer.MAX_VALUE) { 
     throw new IllegalArgumentException(

     "HTTP entity too large to be buffered in memory"); 
    } 

    String charset = getContentCharSet(entity); 

    if (charset == null) { 

     charset = HTTP.DEFAULT_CONTENT_CHARSET; 

    } 

    Reader reader = new InputStreamReader(instream, charset); 

    StringBuilder buffer = new StringBuilder(); 

    try { 

     char[] tmp = new char[1024]; 

     int l; 

     while ((l = reader.read(tmp)) != -1) { 

      buffer.append(tmp, 0, l); 

     } 

    } finally { 

     reader.close(); 

    } 

    return buffer.toString(); 

} 

public static String getContentCharSet(final HttpEntity entity) throws ParseException { 

    if (entity == null) { 
     throw new IllegalArgumentException("HTTP entity may not be null"); 
    } 

    String charset = null; 

    if (entity.getContentType() != null) { 

     HeaderElement values[] = entity.getContentType().getElements(); 

     if (values.length > 0) { 

      NameValuePair param = values[0].getParameterByName("charset"); 

      if (param != null) { 

       charset = param.getValue(); 

      } 

     } 

    } 

    return charset; 

} 

espère que cela aide ...

+0

Êtes-vous testé dans la version Android 4.0? – user1153176

+0

oui. et vous êtes censé voter si ma réponse fonctionne ... – OWADVL

Questions connexes