【问题标题】:how can I fetch data from internet without using third party library in android kotlin?how can I fetch data from internet without using third party library in android kotlin?
【发布时间】:2022-12-01 22:57:12
【问题描述】:

I have a case which I have to do and that case says fetch exchange money currency but dont use third party libraries. I know retrofit and volley but they are third party libaries. I know also async task but they say async task is old and there are issues like memory issues. I can use async task but what do you think about that ? how should I use to fetch data from internet without using third party library ? Do you have an opinion on this?

【问题讨论】:

  • This question is too vague to be on-topic for this site. But here are a couple articles that answer your question. baeldung.com/java-http-request baeldung.com/java-executor-service-tutorial
  • bealdung ... I love random tutorials using fx DataOutputStream for HTTP ... muahahaA data output stream lets an applicationwrite primitive Java datatypes to an output stream in a portable way

标签: android kotlin


【解决方案1】:

You can use this class for HTTP GET requests:

import android.util.Log;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;

public class HttpHandler {

    private static final String TAG = HttpHandler.class.getSimpleName();

    public HttpHandler() {
    }

    public String makeServiceCall(String reqUrl) {
        String response = null;
        try {
            URL url = new URL(reqUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            InputStream in = new BufferedInputStream(conn.getInputStream());
            response = convertStreamToString(in);
        } catch (MalformedURLException e) {
            Log.e(TAG, "MalformedURLException: " + e.getMessage());
        } catch (ProtocolException e) {
            Log.e(TAG, "ProtocolException: " + e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, "IOException: " + e.getMessage());
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
        return response;
    }

    private String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line).append('
');
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return sb.toString();
    }
} 

But you have to run this from background thread. You can use Thread directly but a preferable way for android app written in kotlin will be to use coroutines.

【讨论】:

    猜你喜欢
    • 2022-12-27
    • 2022-12-27
    • 2022-12-19
    • 2022-11-09
    • 2022-12-19
    • 2022-12-27
    • 2022-12-26
    • 2022-11-20
    • 2022-12-01
    相关资源
    最近更新 更多