2017-01-04 48 views
1

我开发了一个应用程序,用于保存通过电话向数据库发出的每个呼叫。这工作得很好,只要通话结束,我就可以获取所有通话详细信息,并运行网络服务进行保存并保存通话。如果未自动保存数据,则与服务器同步

但是,一旦没有网络连接或弱访问互联网,就会出现问题。通话不会被保存。 我正在寻找出路,我可以每天保存所有未保存的电话一次,以便获得100%的电话通话记录。 我试着保存成功登录到HashMap后从服务器接收到的表ID,然后运行一个循环,在该循环中服务将运行,直到找到没有表ID的日志为止,并且服务将在后台运行并使用将运行它的Alarm Manager在每24小时后在背景中。这是我做到的。

public class Senddata extends AsyncTask<String, Void, Void> { 
    String oppurjson; 
    String oppurerror = null; 
    ProgressDialog pd = new ProgressDialog(Create_log.this); 
    String oppurdata = ""; 

    protected void onPreExecute() { 
     // NOTE: You can call UI Element here. 

     //Start Progress Dialog (Message) 

     pd.setMessage("Please wait.."); 
     pd.show(); 
     pd.setCancelable(false); 
     pd.setCanceledOnTouchOutside(false); 

     try { 
      if (sharedpreferences.contains(user)) { 
       idofuser = sharedpreferences.getString(user, ""); 
       // System.out.println("Id of user is"+idofuser); 
      } 
      // Set Request parameter 
      oppurdata += "&" + URLEncoder.encode("user_id", "UTF-8") + "=" + idofuser; 
      oppurdata += "&" + URLEncoder.encode("call_start", "UTF-8") + "=" + calldatechange1; 
      oppurdata += "&" + URLEncoder.encode("duration", "UTF-8") + "=" + callDuration1; 
      oppurdata += "&" + URLEncoder.encode("direction", "UTF-8") + "=" + dir; 
      oppurdata += "&" + URLEncoder.encode("mob", "UTF-8") + "=" + ggg; 

      System.out.println("data going to be saved is-----" + oppurdata); 
     } catch (UnsupportedEncodingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    protected Void doInBackground(String... urls) { 
     /************ Make Post Call To Web Server ***********/ 
     BufferedReader reader = null; 

     // Send data 
     try { 
      // Defined URL where to send data 
      URL url = new URL(urls[0]); 

      // Send POST data request 

      URLConnection conn = url.openConnection(); 

      conn.setConnectTimeout(15000); 

      conn.setReadTimeout(15000); 
      conn.setDoOutput(true); 
      OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
      wr.write(oppurdata); 
      wr.flush(); 

      // Get the server response 

      reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 


      // Read Server Response 
      while ((line = reader.readLine()) != null) { 
       // Append server response in string 
       sb.append(line + " "); 
      } 

      // Append Server Response To Content String 
      oppurjson = sb.toString(); 
     } catch (Exception ex) { 
      oppurerror = ex.getMessage(); 
     } finally { 
      try { 
       reader.close(); 
      } catch (Exception ex) { 
      } 
     } 

     /*****************************************************/ 
     return null; 
    } 

    protected void onPostExecute(Void unused) { 
     // NOTE: You can call UI Element here. 

     // Close progress dialog 
//   pd.dismiss(); 

     if (oppurerror != null) { 
      Toast.makeText(getApplicationContext(), "Unable to create the call log.Try Again later", Toast.LENGTH_LONG).show(); 

     } else { // Show Response Json On Screen (activity) 
      Toast.makeText(getApplicationContext(), "Log created successfully", Toast.LENGTH_LONG).show(); 

      String OutputData = ""; 

      try { 
       //JSONObject jsonRootObject = new JSONObject(oppurjson); 
       JSONObject jsonRootObject = new JSONObject(oppurjson); 
       JSONObject json2 = jsonRootObject.getJSONObject("jsonobj"); 

       callstable = json2.optString("calls_table_id").toString(); 
       status = json2.optString("status").toString(); 

       HashMap<String,String> hm=new HashMap<>(); 
       hm.put("key",callstable); 
       System.out.println("the calls table is"+callstable +"and the status is"+status); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 

     Create_log.this.finish(); 
     moveTaskToBack(false); 
    } 
} 

我已经添加了数据的HashMap,但它的存在返回的错误和数据不获取添加每次。请建议我,因为这是实现我想达到的最佳方式。如果是,我做错了什么?

+0

'保存手机对数据库的每个呼叫? ''数据库?您最好使用'到网络上的数据库'来介绍设置。 – greenapps

+0

对不起。我的意思是网上的数据库。 SQLdatabase。告诉我该怎么做才能做到这一点。? –

回答

0

假设你存储在设备上时调用实体具有一些结构,我建议你为数据库中的每个调用实体添加一个syncStatus域。 syncStatus可以是值“pendingSync”或“synced”。所有的调用都应该保存为“pendingSync”。然后实现一个同步适配器,它将所有具有pendingSync syncStatus的实体上传到您的Web服务,并且在成功上传每个实体时更新它的本地状态为“已同步”。但是当然你可以在这里阅读更多关于https://developer.android.com/training/sync-adapters/creating-sync-adapter.html这里也应该有一个教程。前言:请注意,如果您为应用程序实施了实际的数据库,但我建议您查看Android上的其他持久性选项。我确实看到了你拥有的sharedpreferences变量,但这可能不是你正在做的最好的编码练习。我用一个SQLite数据库来达到类似的目的。如果你是Android的新手,最好的办法是为每一个你正在学习的新功能创建一个新的应用程序,并探索尽可能多的新功能。该功能不会被您试图实现的操作所蒙蔽。

+0

我没有使用Sqlite数据库,所以使用同步适配器将不会很好,我猜。可以帮助我在每24小时后使用散列表来存储和同步数据。 –

相关问题