webservice返回500状态,而它在浏览器中看起来很好json

时间:2016-01-07 06:18:43

标签: android json web-services

我的网络API开发人员为我提供了这个网址 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();

        }

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

1 个答案:

答案 0 :(得分:0)

    #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();
        }