我开发了一个应用程序,用于保存通过电话向数据库发出的每个呼叫。这工作得很好,只要通话结束,我就可以获取所有通话详细信息,并运行网络服务进行保存并保存通话。如果未自动保存数据,则与服务器同步
但是,一旦没有网络连接或弱访问互联网,就会出现问题。通话不会被保存。 我正在寻找出路,我可以每天保存所有未保存的电话一次,以便获得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,但它的存在返回的错误和数据不获取添加每次。请建议我,因为这是实现我想达到的最佳方式。如果是,我做错了什么?
'保存手机对数据库的每个呼叫? ''数据库?您最好使用'到网络上的数据库'来介绍设置。 – greenapps
对不起。我的意思是网上的数据库。 SQLdatabase。告诉我该怎么做才能做到这一点。? –