Java Android API 21异步获取请求不工作

Java Android API 21异步获取请求不工作,java,android,asynchronous,httprequest,Java,Android,Asynchronous,Httprequest,我已经实现了一个Android应用程序,到目前为止还没有出现任何问题 但是,让一个异步GET请求工作已经花了我大约3天的时间,仍然没有任何进展 我尝试使用Android文档并编写了一些代码。。没有结果 然后,我尝试了几乎所有在线解决方案。。同样没有结果 您能给我一个Android API级别21中异步GET请求的解决方案吗 getRequest task = new getRequest(); String contents = null; try {

我已经实现了一个Android应用程序,到目前为止还没有出现任何问题

但是,让一个异步GET请求工作已经花了我大约3天的时间,仍然没有任何进展

我尝试使用Android文档并编写了一些代码。。没有结果 然后,我尝试了几乎所有在线解决方案。。同样没有结果

您能给我一个Android API级别21中异步GET请求的解决方案吗

    getRequest task = new getRequest();
    String contents = null;

    try {
        contents = task.execute(url).get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
这是我的课

public static class getRequest extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        String result = "";
        URL url;
        HttpURLConnection urlConnection;

        try {
            url = new URL(params[0]);
            urlConnection = (HttpURLConnection) url.openConnection();

            urlConnection.setRequestMethod("GET");
            urlConnection.setRequestProperty("headerFieldName", "I took out the fields here since they were personal");

            InputStream in = urlConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(in);

            int data = reader.read();

            while (data != -1) {
                result += (char) data;
                data = reader.read();
            }

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

            return "Failed";
        }
    }
}
公共静态类getRequest扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串结果=”;
网址;
HttpURLConnection-urlConnection;
试一试{
url=新url(参数[0]);
urlConnection=(HttpURLConnection)url.openConnection();
urlConnection.setRequestMethod(“GET”);
setRequestProperty(“headerFieldName”,“我在这里取出了字段,因为它们是个人的”);
InputStream in=urlConnection.getInputStream();
InputStreamReader reader=新的InputStreamReader(in);
int data=reader.read();
while(数据!=-1){
结果+=(字符)数据;
data=reader.read();
}
返回结果;
}捕获(例外e){
e、 printStackTrace();
返回“失败”;
}
}
}
我还尝试使用org.apache,但该库已从API级别23中删除


谢谢大家!

这里是一个使用
HttpsUrlConnection
的简单Get请求示例:

try {
    URL url = new URL(your_url_address);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    try {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line);
        }
        bufferedReader.close();

        //Parse Response
        JSONObject jsonObject = new JSONObject(stringBuilder.toString());

    } finally {
        urlConnection.disconnect();
    }
} catch(Exception e) {
    Log.e("ERROR", e.getMessage(), e);
}

如果
AsyncTask
或网络的样板代码太多,您可以尝试使用网络库,如
OkHttp
改型
,这里是一个使用
HttpsUrlConnection
的简单Get请求示例:

try {
    URL url = new URL(your_url_address);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    try {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line);
        }
        bufferedReader.close();

        //Parse Response
        JSONObject jsonObject = new JSONObject(stringBuilder.toString());

    } finally {
        urlConnection.disconnect();
    }
} catch(Exception e) {
    Log.e("ERROR", e.getMessage(), e);
}

如果
AsyncTask
或一般网络的样板代码太多,您可以尝试使用网络库,如
OkHttp
reformation
您可以找到一个名为OkHttp的库的教程。

您可以找到一个名为OkHttp的库的教程。

请发布一些代码!让我们看看你现在有什么,我们会更容易帮助你。@TodorKostov刚刚添加了代码,谢谢!请发一些代码!让我们看看你现在有什么,我们会更容易帮助你。@TodorKostov刚刚添加了代码,谢谢!