HTTP请求错误Android

时间:2014-03-17 16:05:43

标签: java android http

我正在构建一个Android应用,我正在制作HTTP request。但它显示错误:

Please make sure the Content-Type or Accept of Request Header or Request Parameters is correct!The Request should be like android  

但如果我通过普通的Java程序使用相同的代码,它就能正常工作 这是我的代码:

public static void getJSONFromUrl(String urlStr)  {

        try {

            System.setProperty("http.keepAlive", "false");
         URL url = new URL(urlStr);

            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
            connection.setUseCaches(false); 
            connection.setRequestMethod("GET");
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setConnectTimeout(10000);
            connection.connect();
            System.out.println("Connection :"+connection.getResponseCode());

            String rawObjects = readZipStream(connection.getInputStream());


        } catch (Exception exception) {

            exception.printStackTrace();
        }

    }

     private static String readZipStream(InputStream is) throws IOException,
        ParseException {

            StringBuilder rawObjects = new StringBuilder();

            InputStream zis = new BufferedInputStream(is);
            BufferedReader br = new BufferedReader(new InputStreamReader(zis));
            try {

                String readedLine;
                while ((readedLine = br.readLine()) != null) {
                    System.out.println("readedLine :"+readedLine);
                    if (readedLine != null && readedLine.trim().length() > 0) {
                        rawObjects.append(readedLine);
                    }
                }

            } catch (IOException e) {
                throw new IOException(e.getMessage());
            } finally {
                close(br);
                close(zis);
            }
            return rawObjects.toString();

        }
        private static void close(InputStream inputStream) {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception exception) {

            }
        }

    private static void close(Reader reader) {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (Exception exception) {

        }
    }  

记录o / p:

03-17 21:54:19.021: D/Response:(14409): > Service response code: 404
03-17 21:54:19.021: D/Response:(14409): Service response: Please make sure the Content-Type or Accept of Request Header or Request Parameters is correct!The Request should be like:
03-17 21:54:19.021: D/Response:(14409): ===============Request Method[GET]================
03-17 21:54:19.021: D/Response:(14409): Parameters:
03-17 21:54:19.021: D/Response:(14409): search  query  Required[false]
03-17 21:54:19.021: D/Response:(14409): upcsearch  query  Required[false]
03-17 21:54:19.021: D/Response:(14409): pageno  query  Required[false]
03-17 21:54:19.021: D/Response:(14409): pagesize  query  Required[false]
03-17 21:54:19.021: D/Response:(14409): Headers:
03-17 21:54:19.021: D/Response:(14409): Accept:application/xml, application/json
03-17 21:54:19.021: D/Response:(14409): Service status: 

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

试试这个

DefaultHttpClient httpClient = new DefaultHttpClient();
URI uri = new URI(youruri);
HttpGet httpGet = new HttpGet(uri);
HttpResponse response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
    HttpEntity entity = response.getEntity();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    entity.writeTo(out);
    out.flush();
    out.close();
    String jsonFile = out.toString();
}