简体   繁体   English

在Android应用中使用http客户端模拟表单发布?

[英]Simulate form post using http client in Android app?

So, I'm currently developing an app for a service which has a json-based (unfortunately) read only API . 因此,我目前正在为具有基于json(不幸的是) 只读API的服务开发应用程序。 Retrieving content is no problem at all, however the only way to post content is using a form on their site which location is a PHP script. 检索内容完全没有问题,但是发布内容的唯一方法是在其站点上使用表单,该表单的位置是一个PHP脚本。 The service is open source so I know which fields the form expects, but whatever I send, it always results in a BAD REQUEST . 该服务是开源的,因此我知道表单期望哪些字段,但是无论我发送什么,它总是导致BAD REQUEST

I captured the network traffic inside my browser and as far as I can see, the browser constructs a multipart form request, however when I copy the request and invoke it again using a REST client, a BAD REQUEST gets returned. 我捕获了浏览器内部的网络流量,据我所知,浏览器构造了一个多部分表单请求,但是当我复制该请求并使用REST客户端再次调用该请求时,将返回BAD REQUEST

Is there a way to construct a http request in Android that simulates a form post? 有没有一种方法可以在Android中构造一个模拟表单发布的http请求?

If it's readonly I think you wouldn't be able to make requests with POST (it's assume for editing or adding things). 如果它是只读的,我认为您将无法使用POST发出请求(假定用于编辑或添加内容)。

If you let me make you an advise, I recommend you using this project as a Library. 如果您让我为您提供建议,建议您将此项目用作图书馆。

https://github.com/matessoftwaresolutions/AndroidHttpRestService https://github.com/matessoftwaresolutions/AndroidHttpRestService

It makes you easy deal with apis, control network problems etc. 它使您轻松处理api,控制网络问题等。

You can find a sample of use there. 您可以在此处找到使用示例。

You only have to: 您只需要:

  1. Build your URL 建立您的网址
  2. Tell the component to execute in POST mode 告诉组件以POST模式执行
  3. Build your JSON 建立您的JSON

As I told you, I don't know even if it will work. 就像我告诉你的,我不知道它是否会起作用。

I hope it helps!!! 希望对您有所帮助!!!

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

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