HTTP POST推迟到网上 [英] HTTP Post delayed until online
问题描述
在code我现在有将数据提交成功到数据库时有网络连接。我的应用程序是一个将户外使用,经常在的地方是手机服务非常差。
我的问题是什么是耽误HTTP POST,直到有一个活动的网络连接的最佳方式?我应该创建一个运行一个服务,如果没有当连接可用被惊动连接?传输的数据很小,围绕千字节,所以我只是有应用程序商店,直到设备联机,然后提交数据。这看起来可行?
另外,较差,期权,将是有应用程序商店中的数据,并检查每次它启动,如果有任何数据仍在等待提交,活动连接与提交。
在code我现在有提交的数据如下。如果您有任何建议,那些会比欢迎以及更多。
字符串setPost(上下文的背景下)
{ HttpClient的HttpClient的=新DefaultHttpClient();
HttpPost httppost =新HttpPost(context.getResources()的getString(R.string.url)); 尝试
{
清单< BasicNameValuePair> namevaluepairs中=新的ArrayList< BasicNameValuePair>(22); nameValuePairs.add(新BasicNameValuePair(月,7)); ...等... httppost.setEntity(新UrlEn codedFormEntity(namevaluepairs中)); HTT presponse响应= httpclient.execute(httppost); InputStream为= response.getEntity()的getContent()。
二BufferedInputStream为=新的BufferedInputStream(是);
ByteArrayBuffer BAF =新ByteArrayBuffer(20); INT电流= 0; 而((电流= bis.read())!= - 1)
{
baf.append((字节)电流);
} Toast.makeText(背景下,谢谢您提交,Toast.LENGTH_LONG).show(); 返回新的String(baf.toByteArray()); }
赶上(ClientProtocolException E)
{
Log.e(this.toString(),失败:+ E);
Toast.makeText(背景下,提交失败:(\\ n请举报下方开发商\\错误N+ e.toString(),Toast.LENGTH_LONG).show();
返回(失败:+ E);
}
赶上(IOException异常E)
{
Log.e(this.toString(),失败:+ E);
Toast.makeText(背景下,提交失败:(\\ n请举报下方开发商错误:\\ n+ e.toString(),Toast.LENGTH_LONG).show();
返回(失败:+ E);
}
我应该创建一个运行一个服务,如果没有被惊动连接时连接可用?
块引用>是的。使用
IntentService
来处理所有的网络请求。当活动
要上传/下载任何东西,它通过其请求IntentService
。如果没有网络连接时,请求排队和IntentService
终止本身。另外创建一个
广播接收器
在网络连接变化mainifest其中等待注册。如果变化是一个连接的状态,它应该启动IntentService
用于处理任何排队的网络请求,并再次自我终止。这是有效的,在我的经验效果很好。
The code I currently have submits data successfully to a database when there is a network connection. My app is one that would be used outdoors, frequently in places were cell service is very poor.
My question is what would be the best way to delay the HTTP Post until there is an active network connection? Should I create a service that is run if there is no connection that is alerted when a connection is available? The data transmitted is small, around a kilobyte, so I could just have the app store it until the device is online and then submit the data. Does this seem workable?
Another, poorer, option, would be to have the app store the data and check everytime it is started if there is any data still waiting to be submitted and an active connection to submit it with.
The code I currently have to submit the data is below. If you have any suggestions on it, those would be more than welcome as well.
String setPost(Context context) { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(context.getResources().getString(R.string.url)); try { List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>(22); nameValuePairs.add(new BasicNameValuePair("month", "7")); ... etc ... httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); InputStream is = response.getEntity().getContent(); BufferedInputStream bis = new BufferedInputStream(is); ByteArrayBuffer baf = new ByteArrayBuffer(20); int current = 0; while ((current = bis.read()) != -1) { baf.append((byte)current); } Toast.makeText(context, "thank you for submitting!", Toast.LENGTH_LONG).show(); return new String(baf.toByteArray()); } catch (ClientProtocolException e) { Log.e(this.toString(), "fail: " + e); Toast.makeText(context, "submission failed :(\n please report the error below to developer\n" + e.toString(), Toast.LENGTH_LONG).show(); return ("fail: " + e); } catch (IOException e) { Log.e(this.toString(), "fail: " + e); Toast.makeText(context, "submission failed :(\n please report the error below to developer:\n" + e.toString(), Toast.LENGTH_LONG).show(); return ("fail: " + e); }
解决方案Should I create a service that is run if there is no connection that is alerted when a connection is available?
Yes. Use an
IntentService
to handle all network requests. When anActivity
wants to upload/download anything, it passes its request to theIntentService
. If there is no network connection, the request is queued and theIntentService
terminates itself.Also create a
BroadcastReceiver
registered in the mainifest which 'listens' for network connectivity changes. If the change is to a 'connected' state, it should start theIntentService
which processes any queued network requests and again self-terminates.It's efficient and works well in my experience.
这篇关于HTTP POST推迟到网上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!