2012-03-15 60 views
0

我想解析我的網站序列化的JSON數據:http://demos.brianbuikema.com/apps/soa_services/employees?format=JSON 但我的結果,我回來的是該網站的源代碼:http://demos.brianbuikema.com/apps/soa_services/employees? 因此,某處我的參數format = JSON將被刪除。但我不知道如何在哪裏Httpget刪除參數

這是我的代碼不介意log.d,它只是爲我當我調試調試。 `

HttpClient httpclient = new DefaultHttpClient(); 

    // Prepare a request object 


    HttpGet httpget = new HttpGet("http://demos.brianbuikema.com/apps/soa_services/employees?format=JSON"); 

    String result = null; 
    try { 
     // execute the request 
     Log.d("buh", "2aa"); 
     HttpResponse response = httpclient.execute(httpget); 
          Log.d("buh", "2a"); 
     // Get hold of the response entity 
     HttpEntity entity = response.getEntity(); 
     // If the response does not enclose an entity, there is no need 
     // to worry about connection release 
     Log.d("buh", "2b"); 
     if (entity != null) { 
      // A Simple Response Read 
      Log.d("buh", "2c"); 
      InputStream instream = entity.getContent(); 
      result = convertStreamToString(instream); 
      Log.d("buh",result); 
      // Closing the input stream will trigger connection release 
      instream.close(); 
     } 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return result; 
} 

private static String convertStreamToString(InputStream is) { 
    /* 
    * To convert the InputStream to String we use the 
    * BufferedReader.readLine() method. We iterate until the BufferedReader 
    * return null which means there's no more data to read. Each line will 
    * appended to a StringBuilder and returned as String. 
    */ 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is), 
      8192); 
    StringBuilder sb = new StringBuilder(); 

    String line = null; 
    try { 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    return sb.toString(); 
} ` 

回答

1

的錯誤是從服務器請求此網址時,它會刪除頁眉JSON, 所以你需要添加: httpget.addHeader(new BasicHeader("Accept", "application/json"));下HTTPGET

這裏是全碼:

HttpClient httpclient = new DefaultHttpClient(); 

    // Prepare a request object 
    HttpGet httpget = new HttpGet(url); 
    httpget.addHeader(new BasicHeader("Accept", "application/json")); 
    //httpget.getParams().setParameter("format", "JSON"); 

    Log.d("z",httpget.getURI().toString()); 

    // Execute the request 
    HttpResponse response; 

    String result = null; 
    try { 
     response = httpclient.execute(httpget); 

     // Get hold of the response entity 
     HttpEntity entity = response.getEntity(); 
     // If the response does not enclose an entity, there is no need 
     // to worry about connection release 

     if (entity != null) { 
      // A Simple Response Read 
      InputStream instream = entity.getContent(); 
      result = convertStreamToString(instream); 
      Log.d("z",result); 
      // Closing the input stream will trigger connection release 
      instream.close(); 
     } 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return result; 
} 

private static String convertStreamToString(InputStream is) { 
    /* 
    * To convert the InputStream to String we use the BufferedReader.readLine() 
    * method. We iterate until the BufferedReader return null which means 
    * there's no more data to read. Each line will appended to a StringBuilder 
    * and returned as String. 
    */ 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8192); 
    StringBuilder sb = new StringBuilder(); 

    String line = null; 
    try { 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    return sb.toString(); 
} 
+0

優秀的答案! – 2014-02-19 03:02:07

0

您需要設置名稱值對。 .follow this ..

protected String doInBackground(String... params) { 
     String result = ""; 
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
// REPLACE param [0] or one with your value of variable like _userid or etc.. 
     nameValuePairs.add(new BasicNameValuePair("userid",params[0])); 

     try { 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("http://thisismywebsite.com/mypage.php"); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       is = entity.getContent(); 
     } catch(Exception e){ 
       Log.e("log_tag", "Error in http connection "+e.toString()); 
     } 

     try { 
       BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) { 
         sb.append(line + "\n"); 
       } 
       is.close();  
       result=sb.toString().trim();     
     } catch(Exception e){ 
       Log.e("log_tag", "Error converting result "+e.toString()); 
     } 

     return result; 
    } 

我希望能爲你工作。

+0

您使用: HttpGet httppost = new HttpGet(「http://demos.brianbuikema.com/apps/soa_services/employees」); HttpGet .setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(HttpGet); 您名稱HttpGet httpost但不使用它? en HttpGet.setEntity不存在? – user1264255 2012-03-15 13:33:35

+0

對不起。實際上,我也粘貼了一些我的代碼,這就是爲什麼錯過了一些變量命名,看起來像'HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(「http://demos.brianbuikema.com/apps/soa_services/employees」); httpget.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httpget); ' – 2012-03-15 13:43:31

+0

沒問題:)但仍然是 httpget.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 將不起作用。它得到這個錯誤: 方法setEntity(UrlEncodedFormEntity)是未定義的類型HttpGet – user1264255 2012-03-15 13:46:10