I have created like get parameter in php And I am using Get method to submit to the link. For eg: “http://example.com/subscribe/?act=SubscribeForEmail&EmailAddress=” .
if user type in edittext(say aa@gmail.com) and submit, it will submit as
http://example.com/subscribe/?act=SubscribeForEmail&EmailAddress=aa@gmail.com. How it’s
possible in android?
I tried by using this by an example. But it force closes
The code is as follows:
EditText Ename;
Button btncreate;
String n = null;
String contentOfMyInputStream1;
String output = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Ename = (EditText)findViewById(R.id.msgTextField);
btncreate = (Button)findViewById(R.id.sendButton);
btncreate.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
String st1;
st1 = Ename.getText().toString();
try {
output = "http://example.com/subscribe/?act=SubscribeForEmail&EmailAddress="+st1;
downloadUrl(output);//request been send
}
catch (IOException e) {
e.printStackTrace();
}
if (output != null) {
Toast.makeText(this, output, Toast.LENGTH_SHORT).show();
}
}
public String downloadUrl(String url) throws IOException {
HttpClient httpclient = new DefaultHttpClient();
HttpRequestBase httpRequest = null;
HttpResponse httpResponse = null;
InputStream inputStream = null;
String response = "";
StringBuffer buffer = new StringBuffer();
httpRequest = new HttpGet(url);
httpResponse = httpclient.execute(httpRequest);
inputStream = httpResponse.getEntity().getContent();
int contentLength = (int) httpResponse.getEntity().getContentLength();
if (contentLength < 0) {
// Log.e(TAG, "The HTTP response is too long.");
}
byte[] data = new byte[256];
int len = 0;
while (-1 != (len = inputStream.read(data)) ) {
buffer.append(new String(data, 0, len));
}
inputStream.close();
response = buffer.toString();
return response;
}
I am getting error as
Fatal Exception:main
android.os.NetworkOnMainTreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
at java.net.InetAddress.lookHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:385)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
Please help me to solve the issue.
Thanks in Advance.
As simple as the
Exceptiontells you. You run network stuff on the mainThread. UseAsyncTaskor anew Threadto execute yourdownloadUrlmethod.Use this example code, taken from here:
Call it by these two lines:
This site might help as well.