首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在中解析JSON数据、插入、删除、更新

如何在中解析JSON数据、插入、删除、更新
EN

Stack Overflow用户
提问于 2021-11-23 04:40:00
回答 1查看 234关注 0票数 0

我制作的拉拉api:

  1. 加脂数据、插入、更新、删除此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示例:

代码语言:javascript
运行
复制
[
  {
    "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?

EN

回答 1

Stack Overflow用户

发布于 2021-11-23 07:07:56

对我来说,http://laravelmy.com/public/api/posts网站达不到。

我建议您使用httpUrlConnection从api中获取内容。

代码语言:javascript
运行
复制
// 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;
    }
代码语言:javascript
运行
复制
// Callback class
public interface Callback {
    void onError(final String error);

    void onSuccess(final String result);
}
代码语言:javascript
运行
复制
// 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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70075484

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档