2014年1月14日 星期二

Android中抓取與使用Sessions

Android中抓取與使用Sessions



 


利用java的httpclicent+httpget+thread 讓程式可以執行Sessions和下次使用Sessions。


 









package com.example.framelayout_http_getsessions;
import com.example.framelayout_http_getsessions.MainActivity;
import com.example.framelayout_http_getsessions.MainActivity.ButListener;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.view.Menu;

///////////////////////////////
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

///////////////////////////////////////
import android.view.View;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.MimeTypeMap;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.view.ViewGroup.MarginLayoutParams;
import android.app.ActionBar.LayoutParams;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.net.Uri;
public class MainActivity extends Activity {
FrameLayout FrameLayout1;
DisplayMetrics metrics;
TextView TextView1;
Button Button1;
//WebView WebView1;
String SDPATH;
Thread thread;

int check;
int work_select;//0->get sessions 1->get data
long filesize,downloadsize;
Handler mHandlerTime;
int FILESIZE = 4 * 1024;
String Strresult;
String Strcookie;
String get_result;
private DownloadManager downloadManager;
private SharedPreferences prefs;
private static final String DL_ID = "downloadId";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/////////////////////////////////////////
requestWindowFeature(Window.FEATURE_NO_TITLE); //全螢幕設定
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
metrics = new DisplayMetrics();//抓取螢幕大小
getWindowManager().getDefaultDisplay().getMetrics(metrics);

/////////////////////////////////////////
FrameLayout1 = new FrameLayout(this);
FrameLayout.LayoutParams lparam1 = new FrameLayout.LayoutParams(metrics.widthPixels,metrics.heightPixels);
FrameLayout1.setLayoutParams(lparam1);
this.setContentView(FrameLayout1);

////////////////////////////////////////
Button1=new Button(this);
FrameLayout.LayoutParams layoutParams_Button = new FrameLayout.LayoutParams(metrics.widthPixels/4, metrics.heightPixels/4);
layoutParams_Button.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams_Button.leftMargin = 0;//X
layoutParams_Button.topMargin = 0;//Y
Button1.setLayoutParams(layoutParams_Button);
Button1.setText("下載");
FrameLayout1.addView(Button1);

TextView1=new TextView(this);
FrameLayout.LayoutParams layoutParams_TextView = new FrameLayout.LayoutParams(metrics.widthPixels/4, metrics.heightPixels/4);
layoutParams_TextView.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams_TextView.leftMargin = 0;//X
layoutParams_TextView.topMargin = metrics.heightPixels/4;//Y
TextView1.setLayoutParams(layoutParams_TextView);
TextView1.setText("請點選 下載按鈕");
FrameLayout1.addView(TextView1);

check=0;
filesize=0;
downloadsize=0;
work_select=0;//0->get sessions 1->get data
get_result="error";
mHandlerTime = new Handler();
Button1.setOnClickListener(new ButListener());
}

@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;
}
public Runnable mDownload = new Runnable()
{
@Override
public void run()
{
String REMOTE_FILE_URL=new String();
if(work_select==0)//0->get sessions 1->get data
{
REMOTE_FILE_URL="http://jashliao.pixnet.net/sessions/develop/usb/jl200001";
}
else
{
REMOTE_FILE_URL="http://jashliao.pixnet.net/tw/my_channel_ktv/main";
}
int BUFFER = 1024;

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(REMOTE_FILE_URL);
if(work_select==1)//0->get sessions 1->get data
{
httpGet.setHeader("Cookie", Strcookie);
}

try
{
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();

//=====================
if(work_select==0)//0->get sessions 1->get data
{
Strresult = EntityUtils.toString(entity, HTTP.UTF_8);
//獲得Cookie
List<Cookie> cookies = httpClient.getCookieStore().getCookies();
if (!cookies.isEmpty())
{
get_result="get cookie ok...";
for (int i = 0; i < cookies.size(); i++)
{
Cookie cookie = cookies.get(i);
Strcookie = cookie.getName() + "=" + cookie.getValue() + ";domain=" +
cookie.getDomain();
}
get_result=Strcookie;
}
else
{
get_result="get cookie error";
}
}
else
{
get_result = EntityUtils.toString(entity, HTTP.UTF_8);
}
//=====================
if(work_select==0)//0->get sessions 1->get data
{
work_select++;
}
else
{
work_select=0;
}
}
catch(ClientProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
//httpGet.releaseConnection();
httpClient.getConnectionManager().shutdown();
check=1;
}

}
};
private final Runnable timerRun = new Runnable()
{
public void run()
{
if( (thread!=null) && (check==0))
{
String StrPercent="目前下載百分比:"+String.valueOf( (((float)downloadsize/(float)filesize) *100))+"%";
TextView1.setText("");
TextView1.append("下載中請稍後....\n");
TextView1.append(StrPercent);

mHandlerTime.postDelayed(this, 1000);
}
if( (thread!=null) && (check==1))
{
TextView1.setText("");
TextView1.append("下載完成\n");
TextView1.append("請到指令目錄查看\n");
TextView1.append(get_result);
thread.interrupt();
thread=null;
Button1.setEnabled(true);
}
// 若要取消可以寫一個判斷在這決定��否啟動下一次即可
}
};
class ButListener implements OnClickListener
{
public void onClick(View v)
{
if(work_select==0)//0->get sessions 1->get data
{
Toast.makeText(MainActivity.this, "準備 利用執行緒 取得Sessions... ", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this, "準備 利用執行緒 取得Data...", Toast.LENGTH_SHORT).show();
}
Button1.setEnabled(false);
thread =new Thread(mDownload);
thread.start();
mHandlerTime.postDelayed(timerRun, 1000);
}
}
}


 


 


 


沒有留言:

張貼留言