1

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.

stealthjong
  • 10,858
  • 13
  • 45
  • 84
Shadow
  • 6,864
  • 6
  • 44
  • 93

3 Answers3

1

As simple as the Exception tells you. You run network stuff on the main Thread. Use AsyncTask or a new Thread to execute your downloadUrl method.

Use this example code, taken from here:

private class LongOperation extends AsyncTask<String, Void, String> {

      @Override
      protected String doInBackground(String... params) {
            //Here you put your downloadUrl() method.
            //because this method does stuff in background
            return downloadUrl(params[0])
      }      

      @Override
      protected void onPostExecute(String output) {
          //after downloading is finished, toast it.
          if (output != null)
              Toast.makeText(this, output, Toast.LENGTH_SHORT).show();
      }

      @Override
      protected void onPreExecute() {
      }

      @Override
      protected void onProgressUpdate(Void... values) {
      }
}   

Call it by these two lines:

LongOperation downloadTask = new LongOperation();
downloadTask.execute(output);

This site might help as well.

Community
  • 1
  • 1
stealthjong
  • 10,858
  • 13
  • 45
  • 84
0

Android does not allow you to do long time consuming tasks (such as network operations) on the main (UI) thread. If you need to make http requests you need to create it on a separate thread.

Either use AsyncTask or just spawn it in a new thread.

smk
  • 5,340
  • 5
  • 27
  • 41
0

You cannot perform network IO on the UI thread on Honeycombe. Technically it is possible on earlier versions of Android, but is a really bad idea as it will cause your app to stop responding, and can result in the OS killing your app for being badly behaved. You'll need to run a background process or use AsyncTask to perform your network transaction on a background thread.

There is an article about Painless Threading on the Android developer site which is a good introduction to this, and will provide you with much better depth of answer than can be realistically provided here.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102