2017-04-12 58 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()); 
     }