My Android application need to retreive large data from webserver database via webservice in background process. and save data in local sqlite database.
Problem that I am facing is that my app crashes when someone is viewing the app and at the same time if updation of data is also going on.
It seems that I cannot INSERT data while app is retrieving data from sqlite database.
So, I need functionality like in whatsapp where I can send messages to other users and still messages from different users come which means data is getting inserted in local db in the background. Please help.
package com.ht.scd.scd.helper;
import android.util.Log;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
public class ServiceHandler {
//static String response = null;
public final static int GETRequest = 1;
public final static int POSTRequest = 2;
public ServiceHandler() {
}
/*
* Making service call
*
* @url - url to make request
*
* @method - http request method
*/
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method, null);
}
/*
* Making service call
*
* @url - url to make request
*
* @method - http request method
*
* @params - http request params
*/
public String makeServiceCall(String urladdress, int requestmethod, HashMap<String, String> params) {
URL url;
String responseResult = "";
try {
url = new URL(urladdress);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15001);
conn.setConnectTimeout(15001);
conn.setDoInput(true);
conn.setDoOutput(true);
if (requestmethod == POSTRequest) {
conn.setRequestMethod("POST");
} else if (requestmethod == GETRequest) {
conn.setRequestMethod("GET");
}
if (params != null) {
OutputStream ostream = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(ostream, "UTF-8"));
StringBuilder requestresult = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : params.entrySet()) {
if (first)
first = false;
else
requestresult.append("&");
requestresult.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
requestresult.append("=");
requestresult.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
writer.write(requestresult.toString());
writer.flush();
writer.close();
ostream.close();
}
int reqresponseCode = conn.getResponseCode();
if (reqresponseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line = br.readLine()) != null) {
responseResult += line;
}
} else {
responseResult = "";
}
} catch (Exception e) {
e.printStackTrace();
Log.v("error in ServiceHandler",e.getMessage());
}
return responseResult;
}
}
package com.ht.scd.scd.helper;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import java.util.HashMap;
public class CallWebServiceAsynTaskHelper extends AsyncTask<Void, Void, Void> {
ProgressDialog pDialog;
Context context;
String url;
//List<NameValuePair> params;
HashMap<String, String> params;
int int_method; // GET or POST
boolean progressDialogDisplayFlag = true;
String msgOnDialog = "";
String noInternet_msg = "";
boolean isInternet;
public interface CallWebServiceAsynTaskHelperDoneCallBackInterface {
void CallWebServiceAsynTaskHelperDoneCallBackInterface_callBackMethod(String response);
}
public CallWebServiceAsynTaskHelper(Context context, String url, HashMap<String, String> params, String method,
boolean progressDialogDisplayFlag, String msgOnDialog, String noInternet_msg) {
this.context = context;
this.url = url;
this.params = params;
this.progressDialogDisplayFlag = progressDialogDisplayFlag;
this.msgOnDialog = msgOnDialog;
this.noInternet_msg = noInternet_msg;
if (method.equalsIgnoreCase("GET"))
int_method = 1;
else if (method.equalsIgnoreCase("POST"))
int_method = 2;
pDialog = new ProgressDialog(context);
}
String response = "";
@Override
protected void onPreExecute() {
isInternet = new CheckInternet().isNetworkAvailable(context);
if (progressDialogDisplayFlag) {
pDialog.setMessage(msgOnDialog);
pDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
pDialog.setCancelable(false);
pDialog.show();
}
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
try {
if (isInternet) {// checkInternet
ServiceHandler serverService = new ServiceHandler();
if (params == null) {
response = serverService.makeServiceCall(url, int_method);
} else {
response = serverService.makeServiceCall(url, int_method, params);
}
}
} catch (Exception e) {
e.printStackTrace();
Log.v("Complaint Error", e.toString());
}
return null;
}
@Override
protected void onPostExecute(Void result1) {
super.onPostExecute(result1);
if (pDialog.isShowing())
pDialog.dismiss();
Log.i("Result", response+"");
// {"msg":"success"}
CallWebServiceAsynTaskHelperDoneCallBackInterface ifaceObj = (CallWebServiceAsynTaskHelperDoneCallBackInterface) context;
ifaceObj.CallWebServiceAsynTaskHelperDoneCallBackInterface_callBackMethod(response);
if (!isInternet) {
Toast.makeText(context, noInternet_msg, Toast.LENGTH_LONG).show();
}
}
public String getResponse() {
return response;
}
}
webservice call
url = "http://mysocialbuy.com/webservices/all_super_coupons.php";
callWebServiceAsynTaskHelperDoneCallBackInterface_usingFlag = "couponData";
webserviceTask = new CallWebServiceAsynTaskHelper(this, url, null, "GET", false,
"Please wait", "No Internet");
webserviceTask.execute();
Aucun commentaire:
Enregistrer un commentaire