【问题标题】:webservice returning 500 status while it looks good json in browserwebservice 返回 500 状态,而它在浏览器中看起来不错 json
【发布时间】:2016-01-07 06:18:43
【问题描述】:

我的 web api 开发人员向我提供了这个 url notworking webserice link 我正在尝试像这样使用 AsyncTask 来使用它:

      @Override
        protected String doInBackground(String... params) {
            StringBuffer response = new StringBuffer();




            this.url="http://hellosewa.com/slashapp/public/api/questions/1";

            try {

                URL obj = new URL(url);
                HttpURLConnection con = (HttpURLConnection) obj.openConnection();
                Log.d("async update", "\nSending 'GET' request to URL : " + con);
                Log.d("async update", "\nSending 'GET' request to URL : " + url);



               con.setDoOutput(true);
                DataOutputStream wr = new DataOutputStream(con.getOutputStream());

                wr.flush();
                wr.close();

                int responseCode = con.getResponseCode();
                Log.d("async date", "\nSending 'GET' request to URL : " + url);

                Log.d("async date", "Response Code : " + responseCode);

                BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
                String inputLine;

                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();


            }catch (MalformedURLException e) {
                Log.d("async date", "MalformedUURL Exception" + e);
                e.printStackTrace();
            } catch (IOException e) {
                Log.d("async date", "IO Exception" + e);
                e.printStackTrace();
            }
            Log.d("async date","response=="+response.toString());
            return response.toString();

        }

我不知道为什么它在 webbrowser 中看起来不错的 JSON 时返回 500 响应代码。我的代码可以正常使用此链接 working_link 的 webservice

【问题讨论】:

  • 设置con.setRequestMethod("GET");后试一下,去掉`con.setDoOutput(true);`行

标签: android json web-services


【解决方案1】:
    #For Get Don't Use getOutputStream As GET is for READ#


    ##Remove This Lines Of Code##

    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.flush();
    wr.close();

    ##Also set Some properties After con.setDoOutput(true);##

    con.setRequestMethod("GET");
    con.setRequestProperty("Accept", "application/json");

    Working Code Put It In Main And Try...

try {

            URL url = new URL("http://hellosewa.com/slashapp/public/api/questions/1");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");



            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

            String response = "";
            String lineOutput = "";
            System.out.println("Got Output from Server .... \n");
            while ((lineOutput = br.readLine()) != null) {
                response+=lineOutput;
            }

            System.out.println(response);


            conn.disconnect();








        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        } catch (JSONException e) {
            e.printStackTrace();
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多