我正在抓取网站上的数据。为此我需要POST到这个API 网址:https://api.import.io/store/connector/3b14652e-4785-4402-b1a8-d9363c8e988e/_query?_apikey=
为了获得回复,我还需要发送一些原始数据。数据基本上是从另一个POST请求中检索的响应Json数据。然后,Api将对原始数据执行查询并发送响应。
如何为Android App执行此操作?
这是我的POST
的HTML代码POST /store/connector/3b14652e-4402-b1a8-d9363c8e988e/_login?_apikey=mykey HTTP/1.1
Host: api.import.io
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: 7f95c060-0c59-d92b-xxxx-9b9184527208
{
"username": "user",
"password": "pass"
}
答案 0 :(得分:0)
这样的事情可以帮助您解决问题或让您走上正确的轨道:
URL url = new URL("your_api_url");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
Uri.Builder builder = new Uri.Builder();
builder.appendQueryParameter("data" , "json_value");
String query = builder.build().getEncodedQuery();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(), "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
int response = conn.getResponseCode();
if(response == HttpURLConnection.HTTP_OK)
{
InputStream bis = new BufferedInputStream(conn.getInputStream());
//bis is your json do whatever you want with it
}
conn.disconnect();
答案 1 :(得分:0)
使用Volley post library实现此目的
将依赖关系中的这一行添加到gradle
compile 'com.mcxiaoke.volley:library:1.0.16'
写下这个助手Volley Request课程
public class JSONObjectAuthRequest extends JsonObjectRequest
{
private final Map<String, String> headers;
public JSONObjectAuthRequest(int method, Map<String, String> headers, String url, JSONObject json, Response.Listener<JSONObject> listener, Response.ErrorListener errorListener)
{
super(method, url, json, listener, errorListener);
this.headers = headers;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError
{
return headers;
}
}
并使用上述类传递您的请求
JSONObjectAuthRequest request = new JSONObjectAuthRequest(Request.Method.POST, null, url, postParam, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// write your logic here
Log.d(TAG, "Success");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Failure");
}
});
RequestManager.getInstance(getContext()).addToRequestQueue(request);
这应该有所帮助。
干杯,
沙
答案 2 :(得分:0)
你可以这样做:`
try {
URL reqURL = new URL(link); //the URL we will send the request to
HttpURLConnection request = (HttpURLConnection) (reqURL.openConnection());
request.setDoOutput(true);
request.addRequestProperty("Content-Length", Integer.toString(parameters.length()));
//where parameter is a string written like this : paramKey1=value1¶mKey2=value2
request.addRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //add the content type of the request, most post data is of this type
request.setRequestMethod("POST");
request.connect();
OutputStreamWriter writer = new OutputStreamWriter(request.getOutputStream());
writer.write(parameters);
writer.flush();
writer.close();
int responseCode = request.getResponseCode();
} catch (IOException e) {
e.printStackTrace();
}`
最重要的是,不要忘记将这些行执行到新线程中,因为你无法在ui线程中执行此操作。希望它有所帮助。