2012-01-23 9 views
8

Sto tentando di inviare dati formattati JSON a un server utilizzando Java. Le informazioni arrivano sul server, ma il server risponde con una "Richiesta non valida".Java: Anteprima HttpPost Request

HttpPost httpost = new HttpPost(path); 

    StringEntity se = new StringEntity(JSONRequest); 

    //sets the post request as the resulting string 
    httpost.setEntity(se); 

    //sets a request header so the page receving the request will know what to do with it 
    httpost.setHeader("Accept", "application/json"); 
    httpost.setHeader("Content-type", "application/json;charset=utf8"); 
    HttpResponse response = httpclient.execute(httpost); 

Questa è la configurazione di base della mia richiesta. Ecco l'JSONData:

{"clientApplicationDto":{"AuthenticationToken":"","BrandId":12,"MobileDeviceApplicationId":0},"mobileDeviceInfo":{"CarrierName":"MTN-SA","OsVersion":"2.2.2","ClientApplicationVersion":"TEST","DeviceManufacturer":"HTC","DeviceName":"HTC Desire","DeviceUniqueId":"1e9766fa2ef4c53a","OsName":"8","ClientApplicationTypeId":3}} 

Se questo sembra giusto per te molto, inizierò spamming gli amministratori, ma per ora, ho bisogno di sapere se mi manca qualcosa.

+1

Hm .. per cominciare, 'StringEntity' (o meglio qualsiasi' sottoclasse AbstractHttpEntity') ha un tipo di contenuto, sarà di default è impostato sulla constanti 'HTTP.PLAIN_TEXT_TYPE', usando' HTTP.DEFAULT_CONTENT_CHARSET' come set di caratteri. Potresti impostare il tuo tipo di contenuto chiamando 'se # setContentType (" application/json; charset = utf-8 ")'. Il tuo JSON sembra ben formato. – Jens

+0

Solo per verificare se si tratta di un problema con il post o il codice Java hai provato la richiesta in qualcosa di simile alla console Chrome REST? https://chrome.google.com/webstore/detail/cokgbflfommojglbmbpenpphppikmonn – Dori

risposta

9

ho trovato il problema ... Il server è estremamente sensibile alla intestazione del tipo di contenuto e il formato dei contenuti

httpost.setHeader("Content-type", "application/json;charset=utf8"); 

bisogno di essere cambiato in

httpost.setHeader("Content-type", "application/json; charset=utf-8"); 

e StringEntity SE = new StringEntity (JSONRequest);

necessario per essere cambiato in

 StringEntity se = new StringEntity(JSONRequest,"utf-8"); 

Grazie Jens, che un commento mi ha spinto nella giusta direzione.

+0

Grazie, hai salvato la mia giornata! –

+0

Bello sapere che ti ha aiutato^_ ^ – EZFrag

0

provate questo, è un bene per la u

private static String sendRequestPost(String url, Object obj) { 
     try { 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 

      HttpPost httpost = new HttpPost(url); 
      if (obj != null) { 
       httpost.setEntity(new StringEntity(new Gson().toJson(obj), "utf-8")); 
       System.out.println("Request Json => " + new Gson().toJson(obj)); 
      } 
      httpost.setHeader("Accept", "application/json"); 
      httpost.setHeader("Content-type", "application/json; charset=utf8"); 

      HttpResponse response = httpClient.execute(httpost); 

      HttpEntity responseEntity = response.getEntity(); 
      String strResponse = EntityUtils.toString(responseEntity); 
      return strResponse; 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
      return e.toString(); 
     } 

    }