2016-07-26 29 views
2

が動作していない:このフィールドには、それはJSON values..Toのアクセスが含まれていHttpURLConnectionの応答は次のように私はURL を持っている私のプロジェクトで

localhost:8080/myproject/examples/12 

ヘッダとしてアクセスキーを配置する必要があります。

private String doHttpUrlConnectionAction(String desiredUrl) 
    throws Exception 
    { 
    URL url = null; 
    BufferedReader reader = null; 
    StringBuilder stringBuilder; 

    try 
    { 
     // create the HttpURLConnection 
     url = new URL(desiredUrl); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 

     // just want to do an HTTP GET here 
     connection.setRequestMethod("GET"); 
    // connection.setRequestProperty("Content-Type","application/json"); 
     connection.setRequestProperty("API-KEY", "value"); 

     // uncomment this if you want to write output to this url 
     connection.setDoOutput(true); 

     // give it 15 seconds to respond 
     connection.setReadTimeout(15*1000); 
     connection.connect(); 

     // read the output from the server 
    // reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 


     StringBuilder sb = new StringBuilder(); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
     String line; 
     while ((line = rd.readLine()) != null) { 
      sb.append(line); 
     } 
     return sb.toString(); 

    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
     throw e; 
    } 
    finally 
    { 
     // close the reader; this can throw an exception too, so 
     // wrap it in another try/catch block. 
     if (reader != null) 
     { 
     try 
     { 
      reader.close(); 
     } 
     catch (IOException ioe) 
     { 
      ioe.printStackTrace(); 
     } 
     } 
    } 
    } 

このコードは出力を返します:

Response Code : 200 
<table border="0" cellpadding="4" cellspacing="0"><thead><tr><th>status</th><th>statusCode</th><th>data</th></tr></thead><tbody><tr><td statusCode="200" status="1">Array</td></tr></tbody></table> 

しかし、私はhttclientにこのコードをアクセスしていますが、私は..

正しく値を取得しています私が行っていることということである今

public String ReadHttpResponse(String url){ 
     StringBuilder sb= new StringBuilder(); 
     HttpClient client= new DefaultHttpClient();  
     HttpGet httpget = new HttpGet(url); 
     httpget.addHeader("API-KEY", "value"); 
     try { 
      HttpResponse response = client.execute(httpget); 
      StatusLine sl = response.getStatusLine(); 
      int sc = sl.getStatusCode(); 
      if (sc==200) 
      { 
       HttpEntity ent = response.getEntity(); 
       InputStream inpst = ent.getContent(); 
       BufferedReader rd= new BufferedReader(new InputStreamReader(inpst)); 
       String line; 
       while ((line=rd.readLine())!=null) 
       { 
        sb.append(line); 
       } 
      // System.out.println(sb.toString()); 

      } 
      else 
      { 
       System.out.println("I didn't get the response!"); 


      } 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return sb.toString(); 
    } 

ここでは正しく出力されています。

HttpUrlConnectionの問題はどこですか?私はここで間違って何をしていますか?私はHttpUrlConnectionを使用しなければなりません。皆さんから助けてください。

+0

URLはお使いのブラウザが正しく動作しているかどうかを確認します。 –

+1

リクエストでこのヘッダーを追加する必要があります。urlConnection.setRequestProperty( "Content-Type"、 "application/json; odata = verbose"); urlConnection.setRequestProperty( "Accept"、 "application/json; odata = verbose"); –

+0

サーバー側から200を取得しているため、コードに問題はありません。サーバー側に問題があるので、サーバー側からhtmlコードを取得します。この問題を解決するにはサーバー側を確認してください。 – BarmanInfo

答えて

1

POSTのリクエストとGETのリクエストの両方で、このようなコードロジックを作成できます。コードの複雑さを軽減するのに役立ちます。 GETメソッドとPOSTメソッドの必要に応じて、1つのメソッドを作成してそのメソッドに渡すことができます。

HttpURLConnection urlConnection = null; 
    try { 
     // http client 

     murl=new URL(url); 
     urlConnection = (HttpURLConnection) murl.openConnection(); 

     urlConnection.setRequestProperty("Content-Type", "application/json;odata=verbose"); 
     urlConnection.setRequestProperty("Accept", "application/json;odata=verbose"); 

     // Checking http request method type 
     if (method == POST) { 
      urlConnection.setRequestMethod("POST"); 
      urlConnection.setDoInput(true); 
      urlConnection.setDoOutput(true); 



      if(!jsondata.equals("null")) { 
       OutputStream os = urlConnection.getOutputStream(); 
       BufferedWriter writer = new BufferedWriter(
         new OutputStreamWriter(os, "UTF-8")); 
       writer.write(jsondata); 
       writer.flush(); 
       writer.close(); 
       os.close(); 
      } 


     } else if (method == GET) { 
      // appending params to url 
      urlConnection.setRequestMethod("GET"); 

     } 
     resCode = urlConnection.getResponseCode(); 
     Log.i("TAG", "response code=>" + resCode); 
関連する問題