【问题标题】:Unable to get response in string from weburl?无法从 weburl 获得字符串响应?
【发布时间】:2016-02-03 09:58:05
【问题描述】:
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            String response = "";
            for (String url : urls) {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try {
                    HttpResponse execute = client.execute(httpGet);
                    InputStream content = execute.getEntity().getContent();

                    BufferedReader buffer = new BufferedReader(
                            new InputStreamReader(content));
                    String s = "";
                    while ((s = buffer.readLine()) != null) {
                        response += s;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return response;
        }

        @Override
        protected void onPostExecute(String result) {
           // textView.setText(Html.fromHtml(result));
            Log.i("NNNNNNNNNNNNNN", ""+Html.fromHtml(result));
        }
    }

网址:http://demo.ftheline.com/restaurant/soma-eats/app-order?order_id=710&amount=3&discount_amount=0&discount_code_id=0&restaurant_name=soma-eats&payment_method_nonce=fake-valid-visa-nonce

回复:

{
    "error": 0,
    "message": "Success",
    "url": "http://demo.ftheline.com/restaurant/soma-eats/thank-you/710"
}

如何从给定的网址获取String 的数据?我试过上面的代码并得到HTML 错误响应?请帮忙

【问题讨论】:

  • BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
  • 你尝试了什么?
  • @ρяσѕρєяK 我已经尝试了上面的代码并得到了 html 错误,这些错误来自我的域。

标签: android web-services httpurlconnection


【解决方案1】:

您可以使用以下方式存档。

我在我的应用程序中使用以下方式从 weburi 获取响应。

看看:

private class DownloadWebPageTask extends AsyncTask<String, Void, String> {

String responseString = null;
DefaultHttpClient client = new DefaultHttpClient();
        @Override
        protected String doInBackground(String... urls) {
            String response = "";
            try {
                HttpGet httpGet = new HttpGet(url);
                HttpResponse execute = client.execute(httpGet);
                StatusLine statusLine = execute.getStatusLine();
                if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                    HttpEntity httpEntity = response.getEntity();
                    responseString = EntityUtils.toString(httpEntity);
                }else {
                    //Closes the connection.
                    execute.getEntity().getContent().close();
                    throw new IOException(statusLine.getReasonPhrase());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return responseString;
        }

        @Override
        protected void onPostExecute(String result) {
            if(result!= null){
                // textView.setText(Html.fromHtml(result));
                Log.i("NNNNNNNNNNNNNN", ""+Html.fromHtml(result));
            }   
        }
}

【讨论】:

  • 我已使用此代码,但在我们收到您的任务时出现错误页面。每次我得到错误。你能给我另一个代码或帮助我吗?我已经尝试了很多代码,但无法修复它
  • 我正在从我的域中获取视图源,如下面的 url view-source:demo.ftheline.com
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-31
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 2020-08-25
  • 2021-07-18
  • 2018-02-20
相关资源
最近更新 更多