gpt4 book ai didi

android - Android 中的网络超时

转载 作者:太空宇宙 更新时间:2023-11-03 10:29:52 25 4
gpt4 key购买 nike

我的应用程序和服务器之间的通信超时过多..
我的问题是服务器甚至没有收到消息。所以我猜问题出在我的 Android 代码中,而不是服务器代码中。

这是我的代码:

HttpParams params = new BasicHttpParams();
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry);

//configure timeouts
HttpConnectionParams.setConnectionTimeout(params, 1000 * 1000);
HttpConnectionParams.setSoTimeout(params, 1000 * 1000);

//and finally initialize the http client
this.mClient = new DefaultHttpClient(manager,params);

//init the response handler
this.mResponseHandler = new MyResponseHandler(ctx);




final HttpPost method = new HttpPost(HttpSender.SERVER_URL);

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("message", s));

try {
method.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

Runnable sender = new Runnable(){
@Override
public void run() {
try {
mClient.execute(method, mResponseHandler);

} catch (ClientProtocolException e) {

Log.v(TAG, "unable to send " + s);
e.printStackTrace();

} catch (IOException e) {

Log.v(TAG, "unable to send " + s);
e.printStackTrace();

}
}
};

new Thread(sender).start();

我的代码有问题吗?顺便说一句,它并不总是超时......只有大约 2/3 的时间

编辑:我注意到当我启动电话时它通常工作正常(没有超时)但是之后
一些消息我开始收到超时,所以也许我没有正确关闭某些东西
在我的代码中?

编辑:我注意到的另一件事是,如果消息正常工作,然后我转动手机(从垂直到水平,反之亦然)
方向改变后问题再次出现:

10-03 11:47:46.920: ERROR/[FT]-Server(3563): NetworkStateReceiver :intent: android.net.conn.CONNECTIVITY_CHANGE

感谢您的帮助!
暗里

最佳答案

嗯,我不太确定是什么解决了它,但我认为问题是我没有关闭
无论如何,连接和方法这里是我的代码,如果有人感兴趣的话:

public class HttpSender {

private ResponseHandler<String> mResponseHandler;
private String TAG = "HttpSender";
static final String SERVER_URL = "http://**some ip address**/myServer";

public HttpSender(Context ctx){
//init the response handler
this.mResponseHandler = new MyResponseHandler(ctx);
}

//TODO : see if using postSend is better and dosent cause timeouts?
public void send(final String s,final Context ctx, final int type){
Log.v(TAG, "sending message : " + s + "type message of is " + Integer.toString(type));

//for testing
Toast.makeText(ctx,"sending \n" + s,Toast.LENGTH_LONG).show();
//done for testing

this.postSend(s, ctx, type);


}


public void postSend(final String s,final Context ctx,final int type){
final HttpPost method = new HttpPost(HttpSender.SERVER_URL);

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("message", s));

final DefaultHttpClient client = getNewClient(ctx);

try {
method.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

Runnable sender = new Runnable(){

@Override
public void run() {
try {
client.execute(method, mResponseHandler, new BasicHttpContext());
client.getConnectionManager().shutdown();


} catch (ClientProtocolException e) {
client.getConnectionManager().shutdown();
method.abort();
e.printStackTrace();
} catch (IOException e) {
client.getConnectionManager().shutdown();
method.abort();

Log.v(TAG, "unable to send " + s);
e.printStackTrace();
}
}
}
};
new Thread(sender).start();

}



private DefaultHttpClient getNewClient(Context ctx) {

HttpParams params = new BasicHttpParams();
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry);

//and finally initialize the http client
DefaultHttpClient client = new DefaultHttpClient(manager,params);

return client;
}

关于android - Android 中的网络超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7625238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com