2011-07-27 16 views
6

Böyle bir formata sahip standart olmayan bir istek başlığı eklemem gerekiyor: (X-MMP-Params: fs = 640x0). Ben HTTPClient burada kullanarak kodudur ediyorum:HttpClient isteği için istek başlığı nasıl eklenir?

HttpClient client = new DefaultHttpClient(); 
String getURL = "http://example.com"; 
HttpGet get = new HttpGet(getURL); 
get.setHeader("X-MMP-Params","fs=640x0"); // I set my request header right here 
HttpResponse responseGet = client.execute(get); 

bu bunu yapmak için doğru yolu var mı?

+0

Çalışıyor mu? Test etmek için, 'User-Agent' başlığını ayarlayabilir ve sunucu tarafından alınabilen bir kullanıcı aracısını gösteren bir sayfa indirebilirsiniz (veya kendi PHP sayfanızı oluşturabilirsiniz). Gösterilen kullanıcı aracı, sizin ayarladığınız şey ise, bu yöntem çalışır. –

+3

Bu konuda gezenler ve "Evet, doğru yol muydu?" Diye düşünenler için cevap evet. Bu iyi çalışıyor. – PFranchise

+0

Artık [HttpClient'i kullanmamalısınız] gerçeğinin farkında mısınız? (Http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http- istemci); En azından Android projeleri için değil. – toKrause

cevap

0

olsun isteği HttpGet ile yapılır:

public static InputStream getInputStreamFromUrl(String url) { 
    InputStream content = null; 
    try { 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpResponse response = httpclient.execute(new HttpGet(url)); 
    content = response.getEntity().getContent(); 
    } catch (Exception e) { 
    Log.("[GET REQUEST]", "Network exception", e); 
    } 
    return content; 
} 

POST isteği ile yapılır HttpPost:

public void postData() { 
    // Create a new HttpClient and Post Header 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php"); 

    try { 
     // Add your data 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     nameValuePairs.add(new BasicNameValuePair("id", "12345")); 
     nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!")); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

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

    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
    } 
} 

Ben şahsen Way

public interface YourUsersApi { 

    //You can use rx.java for sophisticated composition of requests 
    @GET("https://stackoverflow.com/users/{user}") 
    public Observable<SomeUserModel> fetchUser(@Path("user") String user); 

    //or you can just get your model if you use json api 
    @GET("https://stackoverflow.com/users/{user}") 
    public SomeUserModel fetchUser(@Path("user") String user); 

    //or if there are some special cases you can process your response manually 
    @GET("https://stackoverflow.com/users/{user}") 
    public Response fetchUser(@Path("user") String user); 

} 
aşağıdaki Güçlendirme denemek reccoment
+0

Başlıkları göremiyorum! –

İlgili konular