我目前有代码可以从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
{" Category":" Textbooks"," Description":"我喜欢数学。"," Price&# 34;:" 10"," Title":" Math Book"," id":" test@test.com 34;} 在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) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71) at sun.reflect.GeneratedMethodAccessor1.invoke(未知来源) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275) 在javafx.fxml.FXMLLoader $ MethodHandler.invoke(FXMLLoader.java:1771) 在javafx.fxml.FXMLLoader $ ControllerMethodEventHandler.handle(FXMLLoader.java:1657) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at com.sun.javafx.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(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 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(EventDispatchChainImpl.java:114) 在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 在com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49) 在javafx.event.Event.fireEvent(Event.java:198) 在javafx.scene.Node.fireEvent(Node.java:8413) 在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.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java:96) at com.sun.javafx.scene.control.skin.BehaviorSkinBase $ 1.handle(BehaviorSkinBase.java:89) at com.sun.javafx.event.CompositeEventHandler $ NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238) at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191) at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59) at com.sun.javafx.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(EventDispatchChainImpl.java:114) at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56) 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(EventDispatchChainImpl.java:114) 在com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74) 在com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54) 在javafx.event.Event.fireEvent(Event.java:198) 在javafx.scene.Scene $ MouseHandler.process(Scene.java:3757) 在javafx.scene.Scene $ MouseHandler.access $ 1500(Scene.java:3485) 在javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762) 在javafx.scene.Scene $ ScenePeerListener.mouseEvent(Scene.java:2494) at com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:380) at com.sun.javafx.tk.quantum.GlassViewEventHandler $ MouseEventNotification.run(GlassViewEventHandler.java:294) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda $ handleMouseEvent $ 354(GlassViewEventHandler.java:416) at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389) at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415) 在com.sun.glass.ui.View.handleMouseEvent(View.java:555) 在com.sun.glass.ui.View.notifyMouse(View.java:937) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda $ null $ 148(WinApplication.java:191) 在java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:0)
setRequestMethod(String method)
似乎需要一个UPPERCASE字符串方法名称。
所以你可以试试conn.setRequestMethod("PUT");