Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1376 connectés 

  FORUM HardWare.fr
  Programmation
  Java

  Probleme sur la fonction get pour un Httprequest

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Probleme sur la fonction get pour un Httprequest

n°2224236
ptitgwend
Posté le 06-04-2014 à 19:34:05  profilanswer
 

Hey, J'ai d'abord télécharger une fonction Httprequest pour effectuer des GET et des PUT. les Put j'ai aucun soucis pour les effectuer mais alors les get cela ne veut pas. Donc j'ai besoin d'un peu d'aide pour effectuer cette tâche
 
j'ai écrit ça comme programme.
public void getData(){
  String fullUrl="http://doc.google.com";
  HttpRequest Req= new HttpRequest();
  String a=Req.sendGet(fullUrl);
  EditText get = (EditText)findViewById(R.id.arr);
     get.setText(a);  
 }
 
le sendGet est  
 public String sendGet(String url) {
        httpGet = new HttpGet(url);  
        try {
            response = httpClient.execute(httpGet);  
        } catch (Exception e) {
            Log.e("Your App Name Here", e.getMessage());
        }
 
        try {
            ret = EntityUtils.toString(response.getEntity());  
        } catch (IOException e) {
            Log.e("Your App Name Here", e.getMessage());
        }
        return ret;
    }
 
Par contre il y a une fonction avec le GET mais je ne sais pas si celui-la sert à qqc dans la réalisation du GET
 
    public InputStream getHttpStream(String urlString) throws IOException {
        InputStream in = null;
        int response = -1;
 
        URL url = new URL(urlString);  
        URLConnection conn = url.openConnection();
 
        if (!(conn instanceof HttpURLConnection))                      
            throw new IOException("Not an HTTP connection" );
 
        try{
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET" );
            httpConn.connect();  
            response = httpConn.getResponseCode();                  
 
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();                                  
            }                      
        } catch (Exception e) {
            throw new IOException("Error connecting" );            
        } // end try-catch
        return in;    
         
    }
 
Merci d'avance ;)


Message édité par ptitgwend le 06-04-2014 à 19:39:17
mood
Publicité
Posté le 06-04-2014 à 19:34:05  profilanswer
 

n°2224250
Devil'sTig​er
Posté le 06-04-2014 à 22:33:03  profilanswer
 

ce httprequest ?
https://github.com/kevinsawicki/http-request
 
Sinon d'ab j'utilise plutôt httpComponents de Apache:
http://hc.apache.org/

n°2224254
ptitgwend
Posté le 06-04-2014 à 22:57:11  profilanswer
 

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
 
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.client.params.CookiePolicy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
 
import android.util.Log;
 
public class HttpRequest{
 
    DefaultHttpClient httpClient;
    HttpContext localContext;
    private String ret;
 
    HttpResponse response = null;
    HttpPost httpPost = null;
    HttpGet httpGet = null;
 
    public HttpRequest(){
        HttpParams myParams = new BasicHttpParams();
 
        HttpConnectionParams.setConnectionTimeout(myParams, 10000);
        HttpConnectionParams.setSoTimeout(myParams, 10000);
        httpClient = new DefaultHttpClient(myParams);        
        localContext = new BasicHttpContext();    
    }
 
    public void clearCookies() {
        httpClient.getCookieStore().clear();
    }
 
    public void abort() {
        try {
            if (httpClient != null) {
                System.out.println("Abort." );
                httpPost.abort();
            }
        } catch (Exception e) {
            System.out.println("Your App Name Here" + e);
        }
    }
 
    public String sendPost(String url, String data) {
        return sendPost(url, data, null);
    }
 
    public String sendJSONPost(String url, JSONObject data) {
        return sendPost(url, data.toString(), "application/json" );
    }
 
    public String sendPost(String url, String data, String contentType) {
        ret = null;
 
        httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109);
 
        httpPost = new HttpPost(url);
        response = null;
 
        StringEntity tmp = null;        
 
        Log.d("Your App Name Here", "Setting httpPost headers" );
 
        httpPost.setHeader("User-Agent", "SET YOUR USER AGENT STRING HERE" );
        httpPost.setHeader("Accept", "text/html,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" );
 
        if (contentType != null) {
            httpPost.setHeader("Content-Type", contentType);
        } else {
            httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded" );
        }
 
        try {
            tmp = new StringEntity(data,"UTF-8" );
        } catch (UnsupportedEncodingException e) {
            Log.e("Your App Name Here", "HttpUtils : UnsupportedEncodingException : "+e);
        }
 
        httpPost.setEntity(tmp);
 
        Log.d("Your App Name Here", url + "?" + data);
 
        try {
            response = httpClient.execute(httpPost,localContext);
 
            if (response != null) {
                ret = EntityUtils.toString(response.getEntity());
            }
        } catch (Exception e) {
            Log.e("Your App Name Here", "HttpUtils: " + e);
        }
 
        Log.d("Your App Name Here", "Returning value:" + ret);
 
        return ret;
    }
 
    public String sendGet(String url) {
        httpGet = new HttpGet(url);  
 
        try {
            response = httpClient.execute(httpGet);  
        } catch (Exception e) {
            Log.e("Your App Name Here", e.getMessage());
        }
 
        //int status = response.getStatusLine().getStatusCode();  
 
        // we assume that the response body contains the error message  
        try {
            ret = EntityUtils.toString(response.getEntity());  
        } catch (IOException e) {
            Log.e("Your App Name Here", e.getMessage());
        }
 
        return ret;
    }
 
    public InputStream getHttpStream(String urlString) throws IOException {
        InputStream in = null;
        int response = -1;
 
        URL url = new URL(urlString);  
        URLConnection conn = url.openConnection();
 
        if (!(conn instanceof HttpURLConnection))                      
            throw new IOException("Not an HTTP connection" );
 
        try{
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET" );
            httpConn.connect();  
 
            response = httpConn.getResponseCode();                  
 
            if (response == HttpURLConnection.HTTP_OK) {
                in = httpConn.getInputStream();                                  
            }                      
        } catch (Exception e) {
            throw new IOException("Error connecting" );            
        } // end try-catch
 
        return in;      
    }
}
 
c'est celui-la


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Java

  Probleme sur la fonction get pour un Httprequest

 

Sujets relatifs
Problème de compréhension de la boucle FORappel fonction à un menuitem d'une toolbar gtk2
Passer "un" argument "deux-en-un" a une fonctionprobleme sur replace dans une macro
Problème latin-1 utf-8 sur restauration de BDDFormulaire de questions pour un résultat en fonction des réponses
Probleme de compatibilité entre sharepoint 2012 et IE11 ?Problème de gallerie
[Problème Maro Vba ] Exporter une colonne en fichier Texte.Problème de doublons
Plus de sujets relatifs à : Probleme sur la fonction get pour un Httprequest


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR