HTTP POST推迟到网上 [英] HTTP Post delayed until online

查看:122
本文介绍了HTTP POST推迟到网上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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 an Activity wants to upload/download anything, it passes its request to the IntentService. If there is no network connection, the request is queued and the IntentService 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 the IntentService which processes any queued network requests and again self-terminates.

It's efficient and works well in my experience.

这篇关于HTTP POST推迟到网上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆