0

I making laravel api like:

  1. Fatching data , Inserting, Updating , Deleting from this api:

    GET http://laravelmy.com/public/api/posts HTTP/1.1

    POST http://laravelmy.com/public/api/posts HTTP/1.1 Content-Type: application/json

    { "user_id": "6", "category_id": "7", "title": "My brand new title", "content": "My brand new content", "excerpt": "excerpt", "thumbnail_path": "thumbnailpath", "status": "1"

    }

    PUT http://laravelmy.com/public/api/posts/1 HTTP/1.1 Content-Type: application/json

    { "user_id": "5", "category_id": "8", "title": "My brand new title up", "content": "My brand new content up", "excerpt": "excerpt", "thumbnail_path": "thumbnail_path", "status": "1" }

    DELETE http://laravelmy.com/public/api/posts/5

JSON Example:

[
  {
    "id": 11,
    "user_id": 6,
    "category_id": 7,
    "title": "My brand new title",
    "content": "My brand new content",
    "excerpt": "excerpt",
    "thumbnail_path": "thumbnailpath",
    "status": "1",
    "created_at": "2021-11-22T11:09:11.000000Z",
    "updated_at": "2021-11-22T11:09:11.000000Z"
  }
]

How can i implement this api for android studio java languages?

  • Does this answer your question? [Using Retrofit in Android](https://stackoverflow.com/questions/26500036/using-retrofit-in-android) – Nitish Nov 23 '21 at 04:46
  • I Don't understand. please explain step by step – Md. Shahinur Islam Nov 23 '21 at 04:53
  • To call api in android, you need to use some http networking library , this is where Retrofit and Volley , Both are networking libraries which are used to get your data using REST based webservice. How to implement retrofit to get data in shown in the link shared above – Nitish Nov 23 '21 at 05:09
  • I want to easy way. Im beginner. – Md. Shahinur Islam Nov 23 '21 at 08:54

1 Answers1

0

For me, The website http://laravelmy.com/public/api/posts can't reach.

I suggest you to use httpUrlConnection to fetch content from api.

// For example, This is a code to fetch data from baserow . Here, urls is your api url, jsonString is json you wanted to post, method can be `GET`, `POST`, etc & jwt Token is authorization token.
// A method from Utility class
public void DoHttpRequest(String urls, String jsonString, String method, String jwtToken, Callback callback) {
        (new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection httpURLConnection = null;
                InputStream inputStream = null;
                try {
                    URL url = new URL(urls);
                    httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setConnectTimeout(5000);
                    httpURLConnection.setRequestProperty("Content-Type", "application/json");
                    httpURLConnection.setDoInput(true);
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setRequestMethod(method);
                    
                    if (!jwtToken.equals("")) {
                        httpURLConnection.setRequestProperty("Authorization", "JWT " + jwtToken);
                    }
                    if (!jsonString.equals("")) {
                        byte[] out = jsonString.getBytes(StandardCharsets.UTF_8);
                        OutputStream outputStream = httpURLConnection.getOutputStream();
                        outputStream.write(out);
                        outputStream.close();
                    }
                    int responseCode = httpURLConnection.getResponseCode();
                    if (responseCode / 100 == 2) {
                        inputStream = new BufferedInputStream(httpURLConnection.getInputStream());
                        String res = convertStreamToString(inputStream);
                        if(res != null){
                            callback.onSuccess(res);
                        }
                    } else {
                        String res = convertStreamToString(httpURLConnection.getErrorStream());
                        if(res != null){
                            callback.onError(res);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    callback.onError(e.getClass().getCanonicalName());
                } finally {
                    try {
                        if(inputStream != null) {
                            inputStream.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if(httpURLConnection != null) {
                        httpURLConnection.disconnect();
                    }
                }
            }
        })).start();
    }
    

    private String convertStreamToString(InputStream is) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        try {
            int len;
            while ((len = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            return baos.toString();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                baos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
// Callback class
public interface Callback {
    void onError(final String error);

    void onSuccess(final String result);
}

// Method to get data, can be your mainActivity class

public void GetData(){
    Utility utility = new Utility();
    utility.DoHttpRequest("http://yourapiwebsite", "", "POST","myJwtToken", new Callback() {
            @Override
            public void onError(String error) {
                activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // You got error,
                            }
                        });
            }

            @Override
            public void onSuccess(String result) {
                activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                // You sucessfully fetched content
                            }
                        });
            }
        });
}

Now, if you wanted to parse JSON & get values from JSON, then you can use `org.json` library. You can find its documentation even in Android Developer website, or you can see [this][1]
 


  [1]: https://abhiandroid.com/programming/json
Oseamiya
  • 67
  • 9