2012-10-10 214 views
1

無效響應我向服務器請求,他應該返回JSON 但結果我得到「404」和...JSON解析從服務器

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <title>Service</title> 
    </head> 
    <body> 
    <div id="content"> 
     <p class="heading1">Service</p> 
     <p>Endpoint not found.</p> 
    </div> 
    </body> 
</html> 

我的要求JSON:

public JSONObject getJSONFromUrl() { 

    // Making HTTP request 
    try { 



     // defaultHttpClient 
     DefaultHttpClient httpClient = getNewHttpClient(); 

     String auth = android.util.Base64.encodeToString(
       ("[email protected]:11111") 
         .getBytes("UTF-8"), android.util.Base64.NO_WRAP); 

     HttpGet get = new HttpGet(
       "https://test2.estafeta.org/mobileestafeta/MobileSurveyReportsService.svc/LoadSurveyTasks?startRowVersion=AAAAAAAAAAA=&count=500"); 

     get.addHeader("Authorization", "Basic " + auth); 

     HttpResponse httpResponse = httpClient.execute(get); 
     //httpResponse.getStatusLine().getStatusCode() 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     is = httpEntity.getContent();   

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    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(); 
     json = sb.toString(); 
    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 

    // try parse the string to a JSON object 
    try { 
     jObj = new JSONObject(json); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    // return JSON String 
    return jObj; 

} 

我需要得到JSON我做錯了嗎?幫助

+0

如果你得到一個404的內容類型,這意味着你是不是調用正確的地址... – njzk2

+0

查詢我已經正確寫入? –

+0

我不認爲你的參數應該寫成這樣。使用UrlEncoder – njzk2

回答

0

也許你忘了設置要收到 get.setHeader("Content-type", "application/json; charset=utf-8");

+0

...或者你只是使用錯誤的網址或你的服務器沒有正確配置或... – Dimanoid

+0

我不知道該怎麼辦...我被賦予了測試任務...問題,我在做什麼錯誤? - 符合「正確指定的參數」。 –