Post Restful service : Service « Network « Android






Post Restful service

  

package app.test;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;

class RestTask extends AsyncTask<HttpUriRequest, Void, String> {

  public static final String HTTP_RESPONSE = "httpResponse";

  private Context mContext;
  private HttpClient mClient;
  private String mAction;

  public RestTask(Context context, String action) {
    mContext = context;
    mAction = action;
    mClient = new DefaultHttpClient();
  }

  public RestTask(Context context, String action, HttpClient client) {
    mContext = context;
    mAction = action;
    mClient = client;
  }

  @Override
  protected String doInBackground(HttpUriRequest... params) {
    try {
      HttpUriRequest request = params[0];
      HttpResponse serverResponse = mClient.execute(request);

      BasicResponseHandler handler = new BasicResponseHandler();
      String response = handler.handleResponse(serverResponse);
      return response;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }

  @Override
  protected void onPostExecute(String result) {
    Intent intent = new Intent(mAction);
    intent.putExtra(HTTP_RESPONSE, result);
    // Broadcast the completion
    mContext.sendBroadcast(intent);
  }

}

public class Test extends Activity {

  private static final String SEARCH_ACTION = "com.examples.rest.SEARCH";
  private static final String SEARCH_URI = "http://search.yahooapis.com/WebSearchService/V1/webSearch";
  private static final String SEARCH_QUERY = "Android";

  private TextView result;
  private ProgressDialog progress;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle("Activity");
    result = new TextView(this);
    setContentView(result);

    try {
      HttpPost searchRequest = new HttpPost(new URI(SEARCH_URI));
      List<NameValuePair> parameters = new ArrayList<NameValuePair>();
      parameters.add(new BasicNameValuePair("appid", "YahooDemo"));
      parameters.add(new BasicNameValuePair("query", SEARCH_QUERY));
      searchRequest.setEntity(new UrlEncodedFormEntity(parameters));

      RestTask task = new RestTask(this, SEARCH_ACTION);
      task.execute(searchRequest);
      progress = ProgressDialog.show(this, "Searching",
          "Waiting For Results...", true);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  @Override
  public void onResume() {
    super.onResume();
    registerReceiver(receiver, new IntentFilter(SEARCH_ACTION));
  }

  @Override
  public void onPause() {
    super.onPause();
    unregisterReceiver(receiver);
  }

  private BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
      if (progress != null) {
        progress.dismiss();
      }
      String response = intent.getStringExtra(RestTask.HTTP_RESPONSE);
      result.setText(response);
    }
  };
}

   
    
  








Related examples in the same category

1.Map Service
2.Widget Service
3.Restful web service task
4.Search with Restful service
5.Tracker Service
6.Weather web service
7.Access a Web service using GET
8.Alarm service
9.Get Running Services Info
10.Local service demo
11.extends Service
12.Service structure
13.Clock Back Service
14.Voice Recognition Service
15.is Connected by Context.CONNECTIVITY_SERVICE
16.BeatService extends Service and Thread
17.TimeoutService (beta). Here you can register a timeout.
18.Timeout Service
19.Screenshot Service