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