• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

How do I write a post request in android

Sadaiyappan

Golden Member
Hi, here is my code so far:


package com.example.work.try1;

import android.widget.Button;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.net.Uri;
import android.content.Intent;
import android.view.View;
import java.i😵utputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class Launch extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);

final View buttonS = findViewById(R.id.button);
buttonS.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Uri uri = Uri.parse("https://www.navanithiastrolife.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});

Button b = (Button) findViewById(R.id.button2);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

EditText e1 = (EditText) findViewById(R.id.editText);
String username = e1.getText().toString();
EditText e2 = (EditText) findViewById(R.id.editText2);
String password = e2.getText().toString();

OutputStream out = null;
try {

URL url = new URL("http://iconinfo.tech/mob_app/");

HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

urlConnection.connect();

urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("username",username);
urlConnection.setRequestProperty("password",password);
urlConnection.setDoOutput(true);

urlConnection.disconnect();

} catch (Exception e) {
System.out.println(e.getMessage());
}
}
});
}
}
 
@purbeast0, we generally consider Google links unhelpful, to the point of trolling. At least linking to a StackOverflow answer (this one being the first link from Google) is more helpful. Discussion of your answer is also encouraged.

And, @Sadaiyappan, please use code tags!

-- Programming Moderator Ken g6
 
Depends on what you are after. There are many ways to do a post request so a lot of it comes down to taste.

I personally like OkHttp + Retrofit. It makes for a really nice API and does a lot of the "right" thing for you automatically.

Other options include

Apache Components Http Client (version 4+)
Async Http Client
HttpUrlConnection (This one is built in)
Jersey (which, if you choose this route, I would have it sit atop something like Netty or the Apache Http Client).

From there, it is mostly just googling the individual APIs and choosing the one right for you.

I've personally used OkHttp, Apache components, and the Jersey client. To me, OkHttp > Apache components > Jersey client.
 
Back
Top