Json数据显示在浏览器上,但通过HttpUrlConnection

时间:2018-06-05 14:04:06

标签: java android json

我试图在“http://www.my currency.net/service/rates”获取一个json数据,其中包含有效的json数据,因为我确认使用了我的浏览器,但当我尝试获取页面时返回错误500使用HttpUrlConnection。请参阅下面的代码截图;注意:我一直在使用代码从相同的URL获取成功响应,但它突然开始返回内部服务器错误。

public JSONObject MakeHttpRequest(){

尝试{

  URL url = new URL("http://www.mycurrency.net/service/rates");

        HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();

        httpCon.setRequestMethod("GET");

        httpCon.connect();

        Log.d("cryptox", String.valueOf(httpCon.getResponseCode() + " : " + httpCon.getResponseMessage() + " : "+ httpCon.getErrorStream()));

        InputStream inputStream = new BufferedInputStream(httpCon.getInputStream());

  //CREATE A BUFFERED READER TO CONVERT THE BYTES inputStream to char BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

        StringBuilder sBuilder = new StringBuilder();
        String line = null;
        jsonString = null;

        try{
            while((line = reader.readLine()) != null){
                sBuilder.append(line).append("\n");
            }
            jsonString = sBuilder.toString();
            jsonObject = new JSONObject(jsonString);
        }
        catch (IOException ex){
            ex.printStackTrace();
        }
        catch (JSONException ex){
            ex.printStackTrace();
        }
        finally {
            inputStream.close();
        }
    }
    catch (MalformedURLException ex){
        ex.printStackTrace();
    }
    catch(ProtocolException ex){
        ex.printStackTrace();
    }
    catch (IOException ex){
        ex.printStackTrace();
    }

    return jsonObject;
}

logcat的

  

06-05 14:24:02.249 23278-23437 / com.cyptoxchange.cryptox D / cryptox:500   : 内部服务器错误 :   缓冲液(com.android.okhttp.internal.http.HttpConnection$ChunkedSource@5f6823).inputStream()   06-05 14:24:02.249 23278-23437 / com.cyptoxchange.cryptox W / System.err:   java.io.FileNotFoundException:http://www.mycurrency.net/service/rates   06-05 14:24:02.253 17887-17887 /? I /:service_to_fd shell:cat   / proc / 23278 / stat 06-05 14:24:02.255 17887-17887 /? I /:子进程   创建成功:local_socket_fd = 23,pid = 23717 06-05 14:24:02.271   17887-23718 /? I /:post waitpid(pid = 23717)status = 0000 06-05   14:24:02.287 23278-23437 / com.cyptoxchange.cryptox W / System.err:at   com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:250)   06-05 14:24:02.287 23278-23437 / com.cyptoxchange.cryptox W / System.err:   在   com.cyptoxchange.cryptox.HttpHandler.MakeHttpRequest(HttpHandler.java:32)

非常感谢任何有关此事业的帮助。谢谢!

0 个答案:

没有答案