How to post params in the body of HTTP post request?
19,666
How about:
public static String makePostRequest(String stringUrl, String payload,
Context context) throws IOException {
URL url = new URL(stringUrl);
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
String line;
StringBuffer jsonString = new StringBuffer();
uc.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
uc.setRequestMethod("POST");
uc.setDoInput(true);
uc.setInstanceFollowRedirects(false);
uc.connect();
OutputStreamWriter writer = new OutputStreamWriter(uc.getOutputStream(), "UTF-8");
writer.write(payload);
writer.close();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(uc.getInputStream()));
while((line = br.readLine()) != null){
jsonString.append(line);
}
br.close();
} catch (Exception ex) {
ex.printStackTrace();
}
uc.disconnect();
return jsonString.toString();
}
Where payload is the body JSON string. You will also need to use an AsyncTask and run the above method in the doInBackground method, like so:
new AsyncTask<String, String, String>() {
@Override
protected String doInBackground(String... params) {
try {
String response = makePostRequest("http://www.example.com",
"{ exampleObject: \"name\" }", getApplicationContext());
return "Success";
} catch (IOException ex) {
ex.printStackTrace();
return "";
}
}
}.execute("");
Now you can use the response you get back from the server as well
Author by
user6456773
Updated on June 05, 2022Comments
-
user6456773 almost 2 years
I have a set of the params, entered by the user and stored here:
RequestParams params = new RequestParams(); params.put("confirmPass", confirmPass); params.put("username", email); params.put("password", password);
Then I instantiate the AsyncHttpClient and implement the required methods:
AsyncHttpClient client = new AsyncHttpClient(); client.get(url, params, new AsyncHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { } @Override public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_LONG).show(); } });
How can I POST the params stored in the body of the request (I am using a server (mocky.io) to mock the whole process)?
-
user6456773 almost 8 yearsThanks a lot for the suggestion! The problem, for me, comes from the mock I need to implement. What I am doing so far - using mocky.io, generating an empty request and giving the parameter of the request as URL in my code. My approach could be completely worng, but what I need to achieve is to post the info, collected from the user, in the body of the server POST request.
-
user6456773 almost 8 yearsThanks for the idea. So, having the method, I invoke in the onSuccess? Something like this? makePostRequest(url,"name:name,email:email",getApplicationContext());
-
user6456773 almost 8 yearsOkey, it's getting clearer :) The AsyncTask i put in the onSuccess, or I don't need to use AsyncHttpClient anymore? What I am trying to do is bost the body of the resuest in mocky.io. Thanks a lot for the help!
-
instanceof almost 8 yearsYou don't have to use AsyncHttpClient anymore :)