public class NetworkService extends AsyncTask {
String url="http://xx.xxx.xxx.xx";;
public AsyncResponse delegate=null;
private StringBuilder builder = new StringBuilder();
String jsonStr = "";
public NetworkService(String api) {
url = url+path+api;
}
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
ServiceHandler sh = new ServiceHandler();
jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
builder.append(jsonStr);
return null;
}
@Override
protected void onPostExecute(Void result) {
//PostPaidFragment obj = (PostPaidFragment)page;
//obj.createList(builder, container, rootView);
delegate.processFinish(builder);
}
}
okey this is the api call, am using
final NetworkService networkCall = new NetworkService(url);
networkCall.delegate = this;
networkCall.execute();
Handler handler = new Handler();
detect = 0;
handler.postDelayed(new Runnable() {
@Override
public void run() {
try
{
if (networkCall.getStatus() == NetworkService.Status.RUNNING)
{
networkCall.cancel(true);
ProgressBar progress = (ProgressBar)findViewById(R.id.progress);
progress.setVisibility(View.INVISIBLE);
Toast toast = Toast.makeText(BBActivity.this, R.string.serverError, Toast.LENGTH_LONG);
toast.show();
}
}
catch(Exception tre)
{
}
}
}, 30000);
basically, i make http call. But if this http is taking long ( 30 seconds in this case), i stop it..and show connection to server error. however, am having problem once this popup occurs...next http call am making is not working either.i am being forced to remove my app, and reinstall it to get it working. any help?
here is service handler.java too
public class ServiceHandler {
static String response = null;
public final static int GET = 1;
public final static int POST = 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 url, int method,
List<NameValuePair> params) {
try {
// http client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
httpPost.addHeader("Cache-Control", "no-cache");
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
// appending params to url
if (params != null) {
String paramString = URLEncodedUtils
.format(params, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("Cache-Control", "no-cache");
httpResponse = httpClient.execute(httpGet);
}
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
}
0 comments:
Post a Comment