Android Open Source - student_travel_app Commit Handler






From Project

Back to project page student_travel_app.

License

The source code is released under:

Apache License

If you think the Android project student_travel_app listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package model;
/*from   www.  j av a 2s . c o m*/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import util.Util;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

import dao.DBManager;

public class CommitHandler implements HttpHandler
{

  /**
   * username&token&name&city&score&eval
   * all url encoded
   * eval is json-format, contains user info
   * */
  @Override
  public void handle(HttpExchange exchange) throws IOException
  {
    try
    {
      InputStream is = exchange.getRequestBody();
      byte [] buf = new byte[Constant.BUFFER_SIZE];
      int read = is.read(buf, 0, Constant.BUFFER_SIZE);
      String data = new String(buf, 0, read);
      
      Util.Log(Constant.LOG_LEVEL_INFO, "Receive commit request : "
      + URLDecoder.decode(URLDecoder.decode(data, "UTF-8"), "UTF-8"), "CommitHandler");
      
      HashMap<String, String> params = Util.getParams(data);
      
      String token = DBManager.getDBManager().getToken(params.get("username"));
      
      JSONObject response = new JSONObject();
      if (token.equals(params.get("token")))
      {
        if (DBManager.getDBManager().addCommit(params.get("name"), 
            params.get("city"), params.get("score"), params.get("eval")))
        {
          response.put("result", "success");
        }
        else
        {
          response.put("result", "fail");
          response.put("reason", "error");
        }
      }
      else
      {
        response.put("result", "fail");
        response.put("reason", "invalid token");
      }
      
      byte [] responseBody = response.toString().getBytes("UTF-8");
      exchange.sendResponseHeaders(200, responseBody.length);
          OutputStream os = exchange.getResponseBody();
          os.write(responseBody);
          os.flush();
          os.close();
          
          Util.Log(Constant.LOG_LEVEL_INFO, "Send commit response : " + new String(responseBody, "UTF-8"), "CommitHandler");
    }
    catch (IOException e)
    {
      Util.Log(Constant.LOG_LEVEL_ERROR, e.getMessage(), "CommitHandler");
    }
    catch (JSONException e)
    {
      Util.Log(Constant.LOG_LEVEL_ERROR, e.getMessage(), "CommitHandler");
    }
  }

}




Java Source Code List

com.piasy.simpletravel.AllPlanActivity.java
com.piasy.simpletravel.HotelDetailActivity.java
com.piasy.simpletravel.LaunchActivity.java
com.piasy.simpletravel.MapViewActivity.java
com.piasy.simpletravel.MyListViewAdapter.java
com.piasy.simpletravel.PopMenu.java
com.piasy.simpletravel.ScrollRefreshListView.java
com.piasy.simpletravel.SearchActivity.java
com.piasy.simpletravel.SettingActivity.java
com.piasy.simpletravel.SignupActivity.java
com.piasy.simpletravel.SimpleTravelApplication.java
com.piasy.simpletravel.SingledayPlanActivity.java
com.piasy.simpletravel.SpotDetailActivity.java
com.piasy.simpletravel.controller.Controller.java
com.piasy.simpletravel.dao.DBHelper.java
com.piasy.simpletravel.dao.DBManager.java
com.piasy.simpletravel.model.CommitModule.java
com.piasy.simpletravel.model.Constant.java
com.piasy.simpletravel.model.Flag.java
com.piasy.simpletravel.model.HotelSeacher.java
com.piasy.simpletravel.model.ListViewItems.java
com.piasy.simpletravel.model.LocationModule.java
com.piasy.simpletravel.model.PlanGenerator.java
com.piasy.simpletravel.model.RouteEntry.java
com.piasy.simpletravel.model.Setting.java
com.piasy.simpletravel.model.SignupModule.java
com.piasy.simpletravel.model.SpotsSeacher.java
com.piasy.simpletravel.model.VerifyModule.java
com.piasy.simpletravel.util.Util.java
dao.DBManager.java
driver.Driver.java
model.CommitHandler.java
model.Constant.java
model.QueryHandler.java
model.Setting.java
model.SignupHandler.java
model.VerifyHandler.java
parser.Driver.java
parser.HotelSeacher.java
util.Util.java