首页 文章

如何使用JSON对象将“Put”请求发送到Java中的JSONPlaceHolder REST API

提问于
浏览
1

我目前有代码可以从JSONPlaceHolder REST API中提取所有信息并将其解析为数组 . 然后我有代码从我的程序中的文本字段获取输入,然后创建一个带有该信息的JSON对象 . 我现在想要获取该JSON对象并将其发送到我已设置的JSONPlaceHolder REST API . 这是我尝试使用的代码,但它不喜欢传递JSON对象作为参数 . 我得到错误:“无法解析方法'写(org.json.JSONObject)' . ”我已经在Stack Overflow上查看了与此类似的其他问题,但它们都没有发送JSON对象 . 这是我目前的代码 .

public static void makePost(JSONObject o) throws Exception {
    URL url = new URL("https://marketplace-7a251.firebaseio.com/Postings.json");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    try {
        conn.setDoOutput(true);
        conn.setRequestMethod("Put");
        OutputStreamWriter out = new OutputStreamWriter(
                conn.getOutputStream());
        out.write(o);             //This is where the problem is
        out.close();
        conn.getInputStream();
    }
    catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

任何帮助表示赞赏!

编辑:这是我现在得到的错误,我在write方法中将o更改为o.toString() .

java.net.ProtocolException: Invalid HTTP method: Put

{“类别”:“教科书”,“描述”:“我喜欢数学 . ”,“价格”:“10”,“ Headers ”:“数学书”,“id”:“test@test.com”} at java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:440)at sun.net.www.protocol.http.HttpURLConnection.setRequestMethod(HttpURLConnection.java:552)at sun.net.www.protocol.https.HttpsURLConnectionImpl.setRequestMethod( HttpsURLConnectionImpl.java:374)at view.Main.makePost(Main.java:122)at view.MarketplaceController.createPostButtonClicked(MarketplaceController.java:65)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl .invoke(NativeMethodAccessorImpl.java:62)位于sun.reflect.misc.Trampoline的java.lang.reflect.Method.invoke(Method.java:498)的sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) . 在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)的sun.reflect.GeneratedMethodAccessor1.invoke(未知来源)调用(MethodUtil.java:71) va.lang.reflect.Method.invoke(Method.java:498)at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)at javafx.fxml.FXMLLoader $ MethodHandler.invoke(FXMLLoader.java:1771)位于com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)的com.unp.javadx文件中的javafx.fxml.FXMLLoader $ ControllerMethodEventHandler.handle(FXMLLoader.java:1657)位于com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager) .java:238)com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)at com.sun.javafx的com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) . event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatc) hChainImpl.java:114)com.sun.javafx上的com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114) .event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74 )at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)at javafx.event.Event.fireEvent(Event.java:198)at javafx.scene.Node.fireEvent(Node.java:8413) at javafx.scene.control.Button.fire(Button.java:185)at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)at com.sun.javafx.scene.control com.sun.javafx.scene.control.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java:89)的com.sun.javafx.event.CompositeEventHandler $ NormalEventHand com中的com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)中的erRecord.handleBubblingEvent(CompositeEventHandler.java:218)位于com的com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)位于com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)的com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher)中的.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) .java:58)com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114),位于com.sun.javafx的com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) . event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java) :114)在com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)atcom.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)at com.sun.javafx.event.EventUtil.fireEvent( EventUtil.java:54)at javafx.scene.Scene的javafx.event.Event.fireEvent(Event.java:198)javafx.scene.Scene的$ MouseHandler.process(Scene.java:3757)$ MouseHandler.access $ 1500( Scene.java:3485)at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)at javafx.scene.Scene $ ScenePeerListener.mouseEvent(Scene.java:2494)at com.sun.javafx.tk.quantum.GlassViewEventHandler com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:294)上的$ MouseEventNotification.run(GlassViewEventHandler.java:380)位于com.sun的java.security.AccessController.doPrivileged(Native Method) .javafx.tk.quantum.GlassViewEventHandler.lambda $ handleMouseEvent $ 354(GlassViewEventHandler.java:416)at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java) :389)com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)位于com.sun.glass的com.sun.glass.ui.View.handleMouseEvent(View.java:555) . ui.View.notifyMouse(View.java:937)位于com.sun.glass.ui.win.WinApplication._runLoop(Native Method)的com.sun.glass.ui.win.WinApplication.lambda $ null $ 148(WinApplication . java:191)at java.lang.Thread.run(Thread.java:745)

1 回答

  • 0

    setRequestMethod(String method) 似乎需要一个UPPERCASE字符串方法名称 .

    所以你可以试试 conn.setRequestMethod("PUT");

相关问题