我正在通过邮件提交我的服务器并发送我的服务器,阅读json。
有一个很大的延迟,我有一个错误 //VERY SLOW HERE =( 1 a 5 min
httpURLConnection c = ( HttpURLConnection ) u.openConnection ();
//or
response = httpclient.execute(httpget); //get or post
和
可能是什么?怎么解决?
执行操作的代码段下方。
这些行在doInBackgrund方法asynktask中。模拟器是4.4.2 google apis或> 4.0(因为推送通知)。
选项1:
URL u = new URL(url);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
//VERY SLOW HERE =( 1 a 5 min open connection
c.setRequestMethod("HEAD");
// c.setRequestProperty("Connection", "Keep-Alive");
c.setRequestProperty("Content-length", "0");
c.setUseCaches(false);
c.setAllowUserInteraction(false);
c.setConnectTimeout(timeout);
c.setReadTimeout(timeout);
c.connect();
int status = c.getResponseCode();
switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(
c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
br.close();
return sb.toString();
}
选项2:
HttpClient httpclient = new DefaultHttpClient();
HttpParams httpParameters = httpclient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
HttpConnectionParams.setTcpNoDelay(httpParameters, true);
HttpPost httpget = new HttpPost(url);
HttpResponse response = null;
try {
response =httpclient.execute(httpget); //VERY SLOW HERE =( 1 a 5 min
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String json = toString(instream);
instream.close();
通常早期工作,模拟器中的时间不超过10秒。请帮帮我=(