2017-01-09 15 views
1

我面臨着我的應用程序的問題。我的應用程序具有自動啓動功能,它總是監聽名爲'BOOT_COMPLETED'的廣播。 我想在我的自定義服務中執行循環請求(startService)每隔5秒鐘。當我啓動應用程序時,一切似乎都很好。重新啓動手機和連接被拒絕:(

當我重新啓動手機時,問題即將到來。因爲應用程序可以自動啓動,所以我什麼也不做,只是看日誌。 this the fail log pic

只有第一個請求會成功,其他人都失敗了(org.apache.http.conn.HttpHostConnectException:連接到http://10.0.27.46:8090拒絕).B當我點擊應用程序圖標啓動它時,每個請求都會成功。

我不能解決問題,請幫助我:)

public void onReceive(Context context, Intent intent) { 
    if (intent.getAction().equals(BOOT_ACTION) || intent.getAction().equals(QUICKBOOT_ACTION) || intent.getAction().equals(REBOOT_ACTION)) { 
     Intent mainActivityIntent = new Intent(); 
     mainActivityIntent.setAction("GrayService"); 
     mainActivityIntent.setPackage("com.wyk.accessibilityservicewx"); 
     context.startService(mainActivityIntent); 
    } 
} 


try { 
    String uriAPI = "http://10.0.27.46:8090/crm/insert_customer_resource.json"; 
    HttpPost httpRequest = new HttpPost(uriAPI); 
    List <NameValuePair> params = new ArrayList <NameValuePair>(); 
    params.add(new BasicNameValuePair("str", "I am Post String")); 
    httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); 
    HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest); 
    if (httpResponse.getStatusLine().getStatusCode() == 200) { 
     String strResult = EntityUtils.toString(httpResponse.getEntity()); 
     LogUtil.log("====success:" + strResult); 
    } else { 
     LogUtil.log("====fail code:" + httpResponse.getStatusLine().getStatusCode()); 
    } 
} catch (Exception e) { 
    e.printStackTrace(); 
    LogUtil.log("====fail code:" + e.toString()); 
} 
+0

每次請求後關閉連接如何? –

回答

0

我固定我的電話系統(小米)casued後援問題。我應該讓我的應用程序設置從自動啓動。