Don’t you know why it is possible to invoke onClickListener only once, on second attempt I should turn on and turn off the Wifi connection on my device. After first click I can’t browse web and read email – so internet connection is blocked.
Code:
package je.net.ua;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class App extends Activity {
Button button;
TextView tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.Button01);
tv = (TextView) findViewById(R.id.TextView01);
Log.d("Dev", "Application started");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
BufferedReader in;
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost("http://www.snee.com/xml/crud/posttest.cgi");
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("fname", "First name"));
postParameters.add(new BasicNameValuePair("lname", "Last name"));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
postParameters);
request.setEntity(formEntity);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
Log.d("Dev", "Line = " + line);
sb.append(line + NL);
}
in.close();
tv.setText(line);
} catch (Exception ex) {
}
}
});
}
}
Debug:
12-27 21:39:16.936: DEBUG/Dev(6568): Line = <html><head><title>posted data</title></head><body><h1>posted data</h1><p>First name: "First+name"</p><p>Last name: "Last+name"</p><p>REQUEST_URI: "/xml/crud/posttest.cgi"</p><p>QUERY_STRING: ""</p><p>CONTENT_LENGTH: "32"</p><p>content passed via STDIN: "fname=First+name&lname=Last+name"</p></body></html>
12-27 21:39:25.566: DEBUG/dalvikvm(85): GC_FOR_MALLOC freed 28784 objects / 1536696 bytes in 109ms
12-27 21:39:34.106: DEBUG/dalvikvm(6072): GC_EXPLICIT freed 1097 objects / 213648 bytes in 67ms
12-27 21:39:42.476: DEBUG/dalvikvm(5056): GC_EXPLICIT freed 1749 objects / 72040 bytes in 53ms
12-27 21:39:47.516: DEBUG/dalvikvm(29993): GC_EXPLICIT freed 1378 objects / 67232 bytes in 91ms
12-27 21:39:50.136: WARN/GTalkService(21165): [GTalkConnection.11] doConnect: caught XMPPError connecting to mtalk.google.com:5228.: (502)
12-27 21:39:50.136: WARN/GTalkService(21165): -- caused by: java.net.SocketException: The operation timed out
It seems as if your OnClickListener is blocked while it waits for a response from
http://www.snee.com/xml/crud/posttest.cgi. Turning the WiFi off breaks the socket connection, so your OnClickListener is no longer blocked at that point.If you were to run the main part of this handler as an AsyncTask, I think you’d find that you can click on the button any number of times (and deluge your server with POSTs as you do so 😉
Update
Responding to your update, especially that web and email are blocked: It seems like there have been other issues with sending POSTs via
DefaultHttpClient. I suggest you look into using AndroidHttpClient, if you’re targeting Froyo and later releases, and be sure to invoke its close() method when you’re finished with the connection. Or, if you need to stick withDefaultHttpClientbecause you need to support earlier releases, try callingclient.getConnectionManager().shutdown()when you’re done with the connection. I can’t promise that these steps would help, but they seem to be good steps to take regardless.(From my comment, below: What happens if you turn off WiFi before starting the program? Does it still block your ability to browse and send email? Also, are you seeing the returned HTML after you call
tv.setText(line)? Lastly, I suggest puttingLog.dcalls inside of your exception handler and after thereadLine()loop, and see if either of them are reached.)