2017-04-12 53 views
-1

的authenticate_user方法旨在創建它通過REST API來獲取在Java中的項目清單:獲取JSON響應,而不是text/html的

ClientConfiguration類:

public HttpResponse clientConfig(String username, String password, String prms) 
    { 
     try 
     { 
      HttpClient httpclient = HttpClients.createDefault(); 
      HttpClientContext httpContext = HttpClientContext.create(); 
      HttpGet httpget = new HttpGet("http://" + _HOST + "/" + prms); 
      List<NameValuePair> nvps = new ArrayList<NameValuePair>(); 
      nvps.add(new BasicNameValuePair("Type", "Basic Auth")); 
      nvps.add(new BasicNameValuePair("Username", username)); 
      nvps.add(new BasicNameValuePair("Password", password)); 
      httpget.setHeader("Authorization", "Basic " + new UrlEncodedFormEntity(nvps)); 
      // Execute and get the response. 
      httpget.setHeader(HttpHeaders.CONTENT_TYPE, "application/json;charset=UTF-8"); 
      HttpResponse response = httpclient.execute(httpget, httpContext); 

      response.setHeader("Content-Type", "application/json"); 
      if (response.getStatusLine().getStatusCode() != 200) 
      { 
       throw new IOException("Non-successful HTTP response: " + response.getStatusLine().getStatusCode() + ":" 
         + response.getStatusLine().getReasonPhrase()); 
      } 
      else if (response.getStatusLine().getStatusCode() == 200) 
      { 
       System.out.println("Response>>>" + response); 
       System.out.println("HTTPResponse: " + response.getStatusLine().getStatusCode() + ":" 
         + response.getStatusLine().getReasonPhrase()); 
       flag = true; 
       return response; 
      } 
      else 
      { 
       System.out.println("Status is not 200"); 
      } 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 

     return null; 
    } 

而我的輸出是html響應而不是html響應我需要JSON格式,它返回我登錄頁面

HTTPResponse: 200:OK 
<!DOCTYPE html> 
<html lang="en"> 
................ </html> 

ContentMimeType: text/html 
SuccessFully login 

任何人都可以幫助我找出問題所在。

+0

你是否在服務器端使用Jersey(它看起來是這樣)?如果是這樣,那麼你需要圍繞你的返回布爾值創建一個包裝器(即一個包含/包裝這個布爾值的簡單類) –

+0

ok,但是我使用了HTTPRequest和Response。 –

+0

是的,我可以看到您使用的是Apache HTTP客戶端,但我詢問了有關服務器框架。 –

回答

-1

嘗試這樣的:

String json_string = EntityUtils.toString(response.getEntity()); 
JSONArray jsonArr = new JSONArray(json_string); 
+0

不,這不能在客戶端處理。 –

+0

謝謝,但這種解決方案對我無效。 –

-1

謝謝朋友們,現在我解決了Jersey框架這一問題。這是它的一個解決方案。

String url = "http://support.sigmastream.com/issues.json"; 
     String output = ""; 
     String authString = username + ":" + password; 
     String authStringEnc = new BASE64Encoder().encode(authString.getBytes()); 
     try 
     { 
      Client restClient = Client.create(); 
      WebResource webResource = restClient.resource(url); 
      ClientResponse resp = webResource.accept("application/json") 
        .header("Authorization", "Basic " + authStringEnc).get(ClientResponse.class); 
      if (resp.getStatus() != 200) 
      { 
       throw new IOException(
         "Non-successful HTTP response: " + resp.getStatus() + " : " + resp.getStatusInfo()); 
      } 
      else 
      { 
       output = resp.getEntity(String.class); 
       System.out.println("response: " + output); 
      } 
     } 
     catch (IOException ie) 
     { 
      System.out.println(ie.getMessage()); 
     }