0

Заранее прошу прощения, если это окажется некорректный код, но ничего лучше "собрать" не получилось.

DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpGet getRequest = new HttpGet(
                "http:");
        getRequest.setHeader("Accept", "application/json");
        // Use GZIP encoding
        getRequest.setHeader("Accept-Encoding", "gzip"); //
        try {
            HttpResponse response = (HttpResponse) httpclient
                    .execute(getRequest);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                Header contentEncoding = response
                        .getFirstHeader("Content-Encoding");
                if (contentEncoding != null
                        && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                    instream = new GZIPInputStream(instream);
                }
                // convert content stream to a String
                String result = readStream(instream);
                instream.close();

                Log.i("JSON", result);
                TextView view = (TextView) findViewById(R.id.result);
                view.setText(result);
                JSONObject jsonObject = new JSONObject(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String readStream(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        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();
    }

Не понимаю в чём причина. Если ЭТО можно исправить, подскажите, что где поменять. Если нет, то у меня есть конкретные вопросы, на которые не смогла найти ничего вразумительного...
Заранее благодарю!

6
  • 1
    отформатируйте код, для начала
    – Stas0n
    28 ноя 2012 в 14:34
  • 2
    что исправить? где ошибки?
    – Gorets
    28 ноя 2012 в 14:36
  • да, не хватает логов...
    – Stas0n
    28 ноя 2012 в 14:37
  • launching has encountered error occurred during the build
    – s01nyshko
    28 ноя 2012 в 14:42
  • Gorets? спасибо за исправление!
    – s01nyshko
    28 ноя 2012 в 14:46

1 ответ 1

1

Помоему вы ищете это:

public XMLParser() {}

    public String getXmlFromUrl(String url) {
        String xml = null;

        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // return XML
        return xml;
    }

это совершенно рабочий код.

12
  • Kerins_Sataier,заменила только Post на Get, не срабатывает, пишет тоже самое при попытке запуска. В Manifeste что-нить особенное прописывать нужно?
    – s01nyshko
    28 ноя 2012 в 15:02
  • права на интернет
    – Gorets
    28 ноя 2012 в 15:04
  • Доколе??? Ставлю на AsyncTask
    – misha_m
    28 ноя 2012 в 15:16
  • Gorets, права прописаны. rasmisha, что именно ДОКОЛЕ? AsyncTask, в смысле думаешь что без него не запускается??? А что поэтапно нельзя всё это реализовывать?
    – s01nyshko
    28 ноя 2012 в 15:35
  • Попробуйте то что тут описано veryant.com/support/phpkb/question.php?ID=157 28 ноя 2012 в 15:36

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.