Android Open Source - student_travel_app Verify Module






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 com.piasy.simpletravel.model;
//  w  ww . j  a v a2s  . c  o m
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.LinkedList;
import java.util.List;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.piasy.simpletravel.controller.Controller;
import com.piasy.simpletravel.util.Util;

public class VerifyModule
{
  String username, password;
  public VerifyModule(String username, String password)
  {
    this.username = username;
    this.password = password;
  }
  
  boolean requested = false;
  public void verify()
  {
    if (!requested)
    {
      Thread verifyThread = new Thread(verifyRunnable);
      verifyThread.start();
    }
  }
  
  Runnable verifyRunnable = new Runnable()
  {
    
    @Override
    public void run()
    {
      requested = true;
      try
      {
        BasicHttpParams httpParams = new BasicHttpParams();  
            HttpConnectionParams.setConnectionTimeout(httpParams, Constant.HTTPCLIENT_CON_TIMEOUT);  
            HttpConnectionParams.setSoTimeout(httpParams, Constant.HTTPCLIENT_READ_TIMEOUT);
            HttpProtocolParams.setUseExpectContinue(httpParams, false);
        HttpClient client = new DefaultHttpClient(httpParams);
        HttpHost host = new HttpHost(Setting.SERVER_IP, Setting.SERVER_PORT, "http");
        HttpPost post = new HttpPost("/verify");
        List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
        params.add(new BasicNameValuePair("username", username));
        long timestamp = System.currentTimeMillis();
        params.add(new BasicNameValuePair("timestamp", Long.toString(timestamp)));
        params.add(new BasicNameValuePair("password", 
            Util.getSHA1Value(password + Long.toHexString(timestamp)))); 
        post.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
        HttpResponse response = client.execute(host, post);
        JSONObject responseInfo = new JSONObject(EntityUtils.toString(response.getEntity(), "UTF-8"));
        String result = responseInfo.getString("result");
        if (result.equals("success"))
        {
          Controller.getController().setSuccessResult(Constant.VERIFY, responseInfo.getString("token"));
          Controller.getController().setCommitted(responseInfo.getJSONArray("committed"));
          Controller.getController().setUserInfo(responseInfo.getJSONObject("info"));
          
          JSONArray recspots = responseInfo.getJSONArray("recspots");
          for (int i = 0; i < recspots.length(); i ++)
          {
            String photoUrlString = recspots.getJSONObject(i).getString("photo");
            URL photoUrl = new URL(photoUrlString);
            HttpURLConnection conn = (HttpURLConnection) photoUrl.openConnection();
            conn.setConnectTimeout(6000);
            conn.setDoInput(true);
            conn.setUseCaches(true);
            InputStream is = conn.getInputStream();
            Bitmap bitmap = BitmapFactory.decodeStream(is);
            is.close();
            File photo = new File(Constant.APP_CACHE_DIR + "/"
                + URLEncoder.encode(photoUrl.getFile(), "UTF-8"));
            if (!photo.exists())
            {
              BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(photo));
              bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
              bos.flush();
              bos.close();
            }
            recspots.getJSONObject(i).put("photo", photo.getAbsolutePath());
          }
          Controller.getController().setRecSpots(recspots);
        }
        else if (result.equals("fail"))
        {
          Controller.getController().setFailResult(Constant.VERIFY, responseInfo.getString("reason"));
        }
      }
      catch (UnsupportedEncodingException e)
      {
        e.printStackTrace();
        Controller.getController().setFailResult(Constant.VERIFY, "??????????????");
      }
      catch (ClientProtocolException e)
      {
        e.printStackTrace();
        Controller.getController().setFailResult(Constant.VERIFY, "??????????????");
      }
      catch (IOException e)
      {
        e.printStackTrace();
        Controller.getController().setFailResult(Constant.VERIFY, "??????????????");
      }
      catch (JSONException e)
      {
        e.printStackTrace();
        Controller.getController().setFailResult(Constant.VERIFY, "??????????????");
      }

      requested = false;
    }
  };
}




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