JSON解析来自服务器的无效响应

时间:2012-10-10 07:58:23

标签: android json

我向服务器发出了请求他应该返回JSON 但结果我得到了“404”和......

<?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(
                ("estafeta@62e12548-0a68-4999-960b-a3bb3c44675c: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我做错了吗?帮助

1 个答案:

答案 0 :(得分:0)

您可能忘记设置要接收的内容类型 get.setHeader("Content-type", "application/json; charset=utf-8");