简体   繁体   English

从应用WP REST API发送GET请求

[英]send GET request from app WP REST API

I have installed the WP REST API plugin on my domain.When I send a GET request to get my sites posts from web browser, it displays a bunch on HTML ( http://honeymarket.org/wp-json/wp/v2/posts ). 我已经在我的域上安装了WP REST API插件。当我发送GET请求以从网络浏览器获取网站帖子时,它在HTML上显示了一堆( http://honeymarket.org/wp-json/wp/v2/帖子 )。 Now how do I send this request from whitin my app? 现在我该如何从我的应用程序发送此请求? Sorry for the dumb question but google is turning up empty. 抱歉,您的问题很愚蠢,但Google却空无一人。

Also how do I get the data from my GET request to show up as posts instead of HTML in my app? 另外,如何从GET请求中获取数据以显示为帖子而不是应用程序中的HTML?

Are you looking for this ? 您在找这个吗? How to call web-Services in java/Android ? 如何在java/Android调用Web服务? If you are looking for this code then explore this => How to Get Web-Service Call using java Code 如果您正在寻找此代码,请探索=> 如何使用Java代码获取Web服务调用

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class NetClientPost {

    // http://localhost:8080/RESTfulExample/json/product/post
    public static void main(String[] args) {

      try {

        URL url = new URL("http://localhost:8080/RESTfulExample/json/product/post");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");

        String input = "{\"qty\":100,\"name\":\"iPad 4\"}";

        OutputStream os = conn.getOutputStream();
        os.write(input.getBytes());
        os.flush();

        if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
            throw new RuntimeException("Failed : HTTP error code : "
                + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(
                (conn.getInputStream())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }

        conn.disconnect();

      } catch (MalformedURLException e) {

        e.printStackTrace();

      } catch (IOException e) {

        e.printStackTrace();

     }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM