我为什么要在httpPost()之后重启Wifi

时间:2010-12-27 19:53:20

标签: java android httpclient

难道你不知道为什么只能调用onClickListener一次,第二次尝试我应该打开并关闭设备上的Wifi连接。首次点击后,我无法浏览网页和阅读电子邮件 - 因此互联网连接被阻止。

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) {
                }

            }
        });


    }
}

调试:

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

1 个答案:

答案 0 :(得分:4)

在等待来自http://www.snee.com/xml/crud/posttest.cgi的响应时,好像您的OnClickListener被阻止了。关闭WiFi会中断套接字连接,因此此时不再阻止OnClickListener。

如果您要将此处理程序的主要部分作为AsyncTask运行,我认为您会发现您可以多次单击该按钮(并在执行此操作时使用POST对您的服务器进行泛洪) ; - )


更新

响应您的更新,尤其是阻止了网络和电子邮件:似乎还有其他问题通过DefaultHttpClient发送POST。如果您要定位Froyo及更高版本,我建议您考虑使用AndroidHttpClient,并确保在完成连接后调用其close()方法。或者,如果您需要坚持DefaultHttpClient,因为您需要支持早期版本,请在完成连接后尝试调用client.getConnectionManager().shutdown()。我不能保证这些步骤会有所帮助,但无论如何它们似乎都是很好的步骤。

(从我的评论中,如果你在启动程序之前关闭WiFi会怎么样?它是否仍会阻止你浏览和发送电子邮件的能力?另外,你打电话给tv.setText(line)后是否看到了返回的HTML?最后,我建议在{1}}循环之后将Log.d调用放在异常处理程序中,并查看是否已达到其中任何一个。)