I need to start and stop the Http request based on AsychronousHttpClient library. i can able to get start the request and getting data from the server but i cannot able to stop the request? Can Anyone suggest any ideas. This is my sample code.
MainActivity:
public class MainActivity extends ActionBarActivity {
Context context;
public NSUCDownload downloadDishes;
public ByteArrayEntity entity;
public String jsonData;
Button send,cancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
send=(Button)findViewById(R.id.send);
cancel=(Button)findViewById(R.id.cancel);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Thread t1 = new Thread(new Runnable() {
public void run()
{
PostMethod();
}
}); t1.start();
}
});
cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// cancel code
Client.cancelAllRequests(context);
}
});
}
void PostMethod()
{
JSONObject objRootNode = new JSONObject();
JSONArray aryChildNodes = new JSONArray();
aryChildNodes.put("some data" );// here parameter for POST data
try {
objRootNode.put("lstDishKeys_in", aryChildNodes);
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println("Json Request for = "+objRootNode.toString());
String postString = objRootNode.toString();
downloadDishes = new NSUCDownload("http:// link which contains value for the Post data",postString,this);
entity = downloadDishes.start();
try {
if(entity!=null)
{
jsonData = EntityUtils.toString(entity);
System.out.println(" Json Data="+jsonData);// here i get final data
}
else
jsonData=null;
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
This is Download Class which is interact with Client class
public class NSUCDownload {
Context context;
public String url;
public GetJsonData objGetJsonData;
int methodtype = 1;
String postData = null;
public final static int GET = 1;
public final static int POST = 2;
ByteArrayEntity responseEntity=null;
Client clientRequest=new Client();
public NSUCDownload(String url, String in_postData, MainActivity context) {
this.url = url;
this.postData = in_postData;
this.methodtype = 2;
this.context=context;
}
public ByteArrayEntity start()
{
objGetJsonData = new GetJsonData();
try {
objGetJsonData.execute().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return objGetJsonData.getData();
}
public class GetJsonData extends AsyncTask<Void,Void,HttpEntity>
{
public ByteArrayEntity getData()
{
return responseEntity;
}
@Override
protected void onPostExecute(HttpEntity result) {
System.out.println("On_Post_Execute_Invoked");
super.onPostExecute(result);
}
@Override
protected HttpEntity doInBackground(Void...none) {
try {
if (methodtype == POST) {
if (postData != null) {
StringEntity entity = new StringEntity(postData);
Client.post(context,url,entity, "application/json", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode,org.apache.http.Header[] headers,byte[] responseBody) {
responseEntity = new ByteArrayEntity(responseBody);
System.out.println(" Success ="+responseBody);
}
@Override
public void onFailure(int statusCode,org.apache.http.Header[] headers,byte[] responseBody, Throwable error) {
System.out.println( "Failure");
}
});
}
}
} catch (IOException e) {
e.printStackTrace();
}
return responseEntity;
}
}
}
Finally this is my Client Class:
public class Client {
static AsyncHttpClient asyncClient = new AsyncHttpClient();
static AsyncHttpClient syncClient = new SyncHttpClient();
static AsyncHttpClient clientReq;
public static void post(Context context, String url, StringEntity entity,String string, AsyncHttpResponseHandler asyncHttpResponseHandler) {
clientReq=getClient();
clientReq.post(context, url, entity, "application/json", asyncHttpResponseHandler );
System.out.println("Context=="+context);
}
private static AsyncHttpClient getClient()
{
// Return the synchronous HTTP client when the thread is not prepared
if (Looper.myLooper() == null)
{
System.out.println(" Getting SyncHttpClient Object");
return syncClient;
}
System.out.println(" Getting AsyncHttpClient Object");
return asyncClient;
}
/*I used this method but still it does not cancel the request. */
public static void cancelAllRequests(Context context)
{
clientReq.cancelRequests(context, true);
//clientReq.cancelAllRequests(true);
System.out.println("CancledRequest ");
}
}