Data fetching from an API in android

how to fetch data from website in android app
how to get data from webservice in android using json
fetch data from server in android
how to include api in android studio
how to call rest api from android app
how to get data from url in android
how to fetch data from api in java
how to call rest api from android app using volley

I have tried to fetch data from an API which has a key. But in the output it says "app key not found".

I have tested it on Postman and it works properly.

Here is my code:

public class fetchData extends AsyncTask<Void,Void,Void> {
    String data="";
    @Override
    protected Void doInBackground(Void... voids) {

        try {
            URL url=new URL("https://app.inyek.com/app_api/api_extra/all_order.php?");

            HttpURLConnection con=(HttpURLConnection) url.openConnection();
            con.setRequestMethod("POST");
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded/json;charset=UTF-8");
            con.setRequestProperty("app_key","whatever");
            con.setDoOutput(true);


}

In PostMan, how did you specify the app key? was it through an HTTP header? (Sorry, I would have added a comment, but I do not have enough reputation)

Or was it specified as a GET parameter? In the latter case, try something like:

URL url=new URL("https://app.inyek.com/app_api/api_extra/all_order.php?app_key=YOUR_KEY");

Android development, fetch data from API and execute a callback |, I love first-class function programming languages (js and python for example), and I use to pass callbacks everywhere, especially when dealing  In order to fetch data from the API, we will need to go to the main activity, and we need to use AsyncTask because API fetching is not possible on the main thread. AsyncTask is simply a thread that does background processes, while showing the results in UI. Using the main thread for network processes will typically crash your app.

Welcome to Stack Exchange! Firstly I'd suggest you don't put your API Key within questions and/or images, as they might be sensitive and can be abused by malicious users. Feel free to edit your question and remove them.

To answer your query, I think you need to write the contents to the http request body in a json format. This can be done as per the guide on the following webpage: https://www.baeldung.com/httpurlconnection-post

In summary, you need to create an output stream and write the contents to it directly.

How to use a web API from your Android app, There are many times when your Android app will need to fetch data from the internet, to provide users with fresh information and/or data. In this article, we discuss how to use a web API from within your Android app, to fetch data for your users. There are two major methods for retrieving data from most web services, XML or JSON .

I strongly suggest you make an Abstract HttpRequestTask which extends AsyncTask. In this abstract ancestor you can make some helper methods for reading your input, something like so:

/**
 * HttpRequestTask is an abstract extension of an AsyncTask for HTTP Requests.
 *
 * @param <P>
 *      Type for parameter(s) to doInBackground (can be Void if none provided)
 * @param <R>
 *      Type for result of request (can be Void if ignored, or using listeners.)
 */
public abstract class HttpRequestTask<P, R> extends AsyncTask<P, Integer, R>
{
    private static final String TAG = "HttpRequestTask";

    // Post form encoded requests, get back JSON response
    private static final RequestMethod DEFAULT_REQUEST_METHOD = RequestMethod.POST;
    private static final String DEFAULT_CONTENT_TYPE = "application/x-www-form-urlencoded;charset=UTF-8;";
    private static final String DEFAULT_ACCEPT = "application/json;";
    private static final int DEFAULT_TIMEOUT = 8000; // 8 seconds
    private static final String CHARSET = "UTF-8";

    protected static final String NULL_CONTEXT = "Context is null.";
    protected static final String INVALID_RESPONSE = "The server did not send back a valid response.";

    // Request methods supported by back-end
    protected enum RequestMethod
    {
        GET("GET"),
        POST("POST");

        private final String method;

        RequestMethod(String method)
        {
            this.method = method;
        }

        @Override
        public String toString()
        {
            return this.method;
        }
    }

    /**
     * ALWAYS use application context here to prevent memory leaks.
     *
     */
    protected HttpRequestTask(@NonNull final Context context)
    {
        this.context = context;
    }

    protected void verifyConnection() throws IOException
    {
        if (!SystemUtil.isInternetAvailable(context))
        {
            throw new IOException("Internet is unavailable.");
        }
    }

    /**
     * Creates and opens a URLConnection for the url parameter, as well as setting request options.
     *
     * @param url
     *      to connect to.
     *
     * @return opened HTTPURLConnection for POSTing data to ctservices.
     */
    protected HttpURLConnection getURLConnection(URL url) throws IOException
    {
        return this.getURLConnection(url, DEFAULT_REQUEST_METHOD, DEFAULT_CONTENT_TYPE,
                DEFAULT_ACCEPT, DEFAULT_TIMEOUT);
    }

    /**
     * Creates and opens a URLConnection for the url parameter, as well as setting request options.
     *
     * @param url
     *      to connect to.
     *
     * @return opened HTTPURLConnection
     */
    protected HttpURLConnection getURLConnection(@NonNull final URL url,
                                                                                             @NonNull final RequestMethod requestMethod,
                                                                                             @NonNull final String contentType,
                                                                                             @Nullable final String accept, final int timeout)
            throws IOException
    {
        verifyConnection();

        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod(requestMethod.toString());
        urlConnection.setRequestProperty("Content-Type", contentType);

        if (accept != null && !accept.isEmpty())
        {
            urlConnection.setRequestProperty("Accept", accept);
        }

        urlConnection.setReadTimeout(timeout);
        urlConnection.setConnectTimeout(timeout);
        urlConnection.setUseCaches(false);
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true);
        return urlConnection;
    }

    /**
     * Creates and opens a URLConnection for the url parameter, but does not set any request options.
     *
     * @param url
     *      to connect to.
     *
     * @return opened HTTPURLConnection without parameters set.
     */
    protected HttpURLConnection getBasicURLConnection(URL url) throws IOException
    {
        if (!SystemUtil.isInternetAvailable(applicationContext.get()))
        {
            throw new IOException("Internet is unavailable.");
        }

        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        return urlConnection;
    }

    /**
     * Write a JSONObject of request parameters to the output stream as form-encoded data.
     *
     * @param urlConnection
     *      opened urlConnection with output enabled (done by getURLConnection).
     * @param params
     *      to write to request.
     *
     * @throws IOException
     *      problem writing to output stream
     */
    protected void writeParams(HttpURLConnection urlConnection, JSONObject params) throws IOException
    {
        OutputStream outputStream = urlConnection.getOutputStream();
        BufferedWriter outWriter = new BufferedWriter(new OutputStreamWriter(outputStream,
                StandardCharsets.UTF_8));

        String urlParams = this.encodeJSONObject(params);

        outWriter.write(urlParams);
        outWriter.flush();
        outWriter.close();
        outputStream.close();
    }

    /**
     * Reads the response of a URLConnection from the input stream and puts it in a string.
     *
     * @param urlConnection
     *      opened urlConnection with input enabled (done by getURLConnection).
     *
     * @return response string
     *
     * @throws IOException
     *      problem reading input stream
     */
    protected String readResponse(HttpURLConnection urlConnection) throws IOException
    {
        InputStream inputStream = null;

        try
        {
            /* If we failed to connect will throw a SocketResponseTimeoutException,
             * which is an IOException. */
            int responseCode = urlConnection.getResponseCode();

            if (HttpURLConnection.HTTP_OK != responseCode)
            {
                throw new IOException("Bad response code - " + responseCode);
            }

            inputStream = urlConnection.getInputStream();
            final String response = parseInputStream(inputStream);
            urlConnection.disconnect();
            return response;
        }
        finally
        {
            if (inputStream != null)
            {
                try
                {
                    inputStream.close();
                }
                catch (Exception e) {}
            }
        }
    }

    protected Context getContext()
    {
        return this.context;
    }

    protected String getString(final int resId)
    {
        return getContext().getString(resId);
    }

    /**
     * Encodes a JSONObject as a form-data URL string.
     *
     * @param jo
     *      to encode
     *
     * @return encoded URL string
     */
    private String encodeJSONObject(JSONObject jo)
    {
        StringBuilder sb = new StringBuilder();
        boolean first = true;
        Iterator<String> itr = jo.keys();
        String key;
        Object val;

        try
        {
            while (itr.hasNext())
            {
                key = itr.next();
                val = jo.get(key);

                if (first)
                {
                    first = false;
                }
                else
                {
                    sb.append('&');
                }

                sb.append(URLEncoder.encode(key, CHARSET));
                sb.append('=');
                sb.append(URLEncoder.encode(val.toString(), CHARSET));
            }
        }
        catch (JSONException | UnsupportedEncodingException e) {}

        return sb.toString();
    }

    private String parseInputStream(InputStream is) throws IOException
    {
        BufferedReader br = null;

        try
        {
            br = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line;

            while ((line = br.readLine()) != null)
            {
                sb.append(line);
            }

            return sb.toString();
        }
        finally
        {
            if (br != null)
            {
                try
                {
                    br.close();
                }
                catch (Exception e) {}
            }
        }
    }

    /**
     * Merges any properties of b into a that don't already have a key match in a.
     *
     * @param a
     *      merging to
     * @param b
     *      merging from
     *
     * @return a with any unique values from b
     */
    protected JSONObject mergeJSONObjects(JSONObject a, JSONObject b)
    {
        if (b == null)
        {
            return a;
        }
        if (a == null)
        {
            return b;
        }

        try
        {
            Iterator<String> bItr = b.keys();
            String key;
            while (bItr.hasNext())
            {
                key = bItr.next();
                if (!a.has(key))
                {
                    a.put(key, b.get(key));
                }
            }

            return a;
        }
        catch (Exception ex)
        {
            Log.e(TAG, ex.getClass().getSimpleName() + " in mergeJSONObjects: " + ex.getMessage() +
                    '\n' + Log.getStackTraceString(ex));
            return a;
        }
    }
}

Then you can extend your HttpRequestTask to easily make network requests:

public class ExampleNetworkTask extends HttpRequestTask<Void, Void>
{
    private static final String TAG = "ExampleNetworkTask";

    private final SimpleListener successListener;
    private final StringListener errorListener;
    private final String servicesUrl;

    public static void start(@NonNull final Context context,
                             @Nullable final SimpleListener successListener,
                             @Nullable final StringListener errorListener)
        throws IllegalArgumentException
    {
        if (context == null)
        {
            throw new IllegalArgumentException(NULL_CONTEXT);
        }

        new ExampleNetworkTask(context, successListener, errorListener).execute();
    }

    private ExampleNetworkTask(@NonNull final Context context,
                               @Nullable final SimpleListener successListener,
                               @Nullable final StringListener errorListener)
    {
        super(context);

        this.servicesUrl = SystemUtil.getServiceUrl(getContext(), R.string.example_service);
        this.successListener = successListener;
        this.errorListener = errorListener;
    }

    @Override
    protected Void doInBackground(Void... voids)
    {
        try
        {
            final HttpURLConnection urlConnection = super.getURLConnection(new URL(servicesUrl));

            final JSONObject params = new JSONObject();

            // Add params
            params.put("app_key", appKey);
            params.put("order_number", orderNumber);
            // ...

            // Send request, read parse response
            super.writeParams(urlConnection, params);
            final String response = super.readResponse(urlConnection);
            final JSONObject responseObj = new JSONObject(response);

            // Handle response
        }
        catch (Exception ex)
        {
            final String msg = ex.getLocalizedMessage();
            Log.e(TAG, ex.getClass().getSimpleName() + ": " + msg  + '\n' +
                    Log.getStackTraceString(ex));

            // Handle network exceptions and other exceptions here.
        }
        return null;
    }
}

How To Fetch Data From Web API In Android, In this article, we will discuss how to use a Web API within your Android app, to fetch data for your users. There are two major methods for  Data fetching from an API in android. Ask Question Viewed 69 times 0. I have tried to fetch data from an API which has a key. But in the

Thank you guys! finally i got the answer using OkHttpClient. here is the code:

 OkHttpClient client = new OkHttpClient();

            MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
            RequestBody body = RequestBody.create(mediaType, "app_key=whatever");
            Request request = new Request.Builder()
                    .url("https://app.inyek.com/app_api/api_extra/all_order.php")
                    .post(body)
                    .addHeader("Content-Type", "application/x-www-form-urlencoded")
                    .addHeader("key", "whatever")
                    .addHeader("cache-control", "no-cache")
                    .addHeader("Postman-Token", "whatever")
                    .build();

            Response response = client.newCall(request).execute();

Fetching data via a simple API request with Volley., Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. By default all the volley network calls works  This specific kind of data don't vary in hours so it's better to have some sort of cache. If data were not retrieved yet, then FetchData is instantiated and executed, otherwise the data are already present and can be rendered. When FetchData is instantiated we pass the API url and a reference to the current activity object.

Fetch data from JSON API , parse it and show in custom Listview , The data will be shown in a custom listview . We are using HttpURLConnection library and Android's AsyncTask library. This tutorial will be very  ANDROID:FETCHING DATA FROM THINGSPEAK TO ANDROID March 11, 2018 Shaheer ANDROID 8 Comments You can fetch the value from Thingspeak using the following code.You just need to change the channel id and the read API key of your channel in Thingspeak.

Android Development Tutorial, Running on the main thread is convenient for populating UI controls with received data, as you can freely modify UI controls directly from your response handler,  In this JSON Parsing in Android Tutorial we will learn Fetching Data from MySQL and Displaying it in ListView using JSON Format. 3.3 Consuming the API to Fetch

JSON DATA FETCHING AND PARSING FROM URL ANDROID , Developer guides · Design guides · API reference · Samples · Android Studio · Google Developers. Android · Chrome · Firebase · Google Cloud  After reading this blog we will be able to fetch data from Firebase Realtime Database. This data may include simple data types, such as Strings, or complex data types such as Java objects. Pre-requisites. We need to have a Android Studio project and Firebase project ready in order to use the Firebase Realtime Database.

Comments
  • I hope that the key you have provided in your answer is a random string and not your actual key. For security reason.
  • I suggest you to try inspect your raw POST request and see if id does match with the one Postman does.
  • @shadowsheep yah thats a random key and thats not actual key .
  • Okay. That's sound good. Btw if it would happen to add sensitive information by mistake, pls refer to this as a remedy.
  • sir it is post method, please check 1st portion of my question which is "enter image description here". i have added a screen shot of postman there. and i have already tried through HTTP header but it does not work.
  • sir actually my question is where to put the key? in your link there is no key.
  • As far as I understand from your image, the API key is passed as a parameter within the http POST request body. As such, you will likely need to format it as such. Such a string could be formatted as: "{\"api_key\": \"somekeyhere\"}"
  • From the page I linked, you can see how to write such a json string to the request body in sections 2.7 and 2.8
  • sir i dont have any problem about representing json value ,i just want to know where should i place the app key???
  • You would just add it to your params like so: params.put("app_key", appKey);