我制作的拉拉api:
获取http://laravelmy.com/public/api/posts HTTP/1.1
POST http://laravelmy.com/public/api/posts HTTP/1.1内容-类型: application/json
{ "user_id":"6“、"category_id":"7”、“标题”:“我的全新标题”、“内容”:“我的全新内容”、“摘录”:“节选”、"thumbnail_path":“拇指指甲路径”、“状态”:"1“
}
放置http://laravelmy.com/public/api/posts/1 HTTP/1.1内容-类型: application/json
{ "user_id":"5","category_id":"8",“标题”:“我的全新标题”,“内容”:“我的全新内容”,“摘录”:“摘录”,"thumbnail_path":"thumbnail_path",“状态”:"1“}
删除http://laravelmy.com/public/api/posts/5
JSON示例:
[
{
"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"
}
]
我如何为语言实现这个api?
发布于 2021-11-23 07:07:56
对我来说,http://laravelmy.com/public/api/posts
网站达不到。
我建议您使用httpUrlConnection
从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
https://stackoverflow.com/questions/70075484
复制相似问题