Testing for Writing Code

Posted: April 28, 2013 in Android


package com.test.guide;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

/**
* Android µœ÷◊Û”“ª¨∂Ø÷∏“˝–ßπ˚
* @Description: Android µœ÷◊Û”“ª¨∂Ø÷∏“˝–ßπ˚

* @File: MyGuideViewActivity.java

* @Package com.test.guide

* @Author Hanyonglu

* @Date 2012-4-6 œ¬ŒÁ11:15:18

* @Version V1.0
*/
public class MyGuideViewActivity extends Activity {
private ViewPager viewPager;
private ArrayList pageViews;
private ImageView imageView;
private ImageView[] imageViews;
// ∞¸π¸ª¨∂ØÕº∆¨LinearLayout
private ViewGroup main;
// ∞¸π¸–°‘≤µ„µƒLinearLayout
private ViewGroup group;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// …Ë÷√Œfi±ÍÂ¥∞ø⁄
requestWindowFeature(Window.FEATURE_NO_TITLE);

LayoutInflater inflater = getLayoutInflater();
pageViews = new ArrayList();
pageViews.add(inflater.inflate(R.layout.item05, null));
pageViews.add(inflater.inflate(R.layout.item06, null));
pageViews.add(inflater.inflate(R.layout.item01, null));
pageViews.add(inflater.inflate(R.layout.item02, null));
pageViews.add(inflater.inflate(R.layout.item03, null));
pageViews.add(inflater.inflate(R.layout.item04, null));

imageViews = new ImageView[pageViews.size()];
main = (ViewGroup)inflater.inflate(R.layout.main, null);

group = (ViewGroup)main.findViewById(R.id.viewGroup);
viewPager = (ViewPager)main.findViewById(R.id.guidePages);

for (int i = 0; i < pageViews.size(); i++) {
imageView = new ImageView(MyGuideViewActivity.this);
imageView.setLayoutParams(new LayoutParams(20,20));
imageView.setPadding(20, 0, 20, 0);
imageViews[i] = imageView;

if (i == 0) {
//ƒ¨»œ—°÷–µ⁄“ª’≈Õº∆¨
imageViews[i].setBackgroundResource(R.drawable.page_indicator_focused);
} else {
imageViews[i].setBackgroundResource(R.drawable.page_indicator);
}

group.addView(imageViews[i]);
}

setContentView(main);

viewPager.setAdapter(new GuidePageAdapter());
viewPager.setOnPageChangeListener(new GuidePageChangeListener());
}

// ÷∏“˝“≥√Ê ˝æ›  ≈‰∆˜
class GuidePageAdapter extends PagerAdapter {

@Override
public int getCount() {
return pageViews.size();
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}

@Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return super.getItemPosition(object);
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
// TODO Auto-generated method stub
((ViewPager) arg0).removeView(pageViews.get(arg1));
}

@Override
public Object instantiateItem(View arg0, int arg1) {
// TODO Auto-generated method stub
((ViewPager) arg0).addView(pageViews.get(arg1));
return pageViews.get(arg1);
}

@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub

}

@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}

@Override
public void startUpdate(View arg0) {
// TODO Auto-generated method stub

}

@Override
public void finishUpdate(View arg0) {
// TODO Auto-generated method stub

}
}

// ÷∏“˝“≥√Ê∏¸∏ƒ ¬º˛º‡Ã˝∆˜
class GuidePageChangeListener implements OnPageChangeListener {

@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub

}

@Override
public void onPageSelected(int arg0) {
for (int i = 0; i < imageViews.length; i++) {
imageViews[arg0].setBackgroundResource(R.drawable.page_indicator_focused);

if (arg0 != i) {
imageViews[i].setBackgroundResource(R.drawable.page_indicator);
}
}
}
}
}

MD5 in Java

Posted: October 15, 2012 in Java

 

 

Android Mysql

Posted: October 14, 2012 in Android

Android <=> Mysql

Main.java

——————————————

package com.example.databasetest_1;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
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.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

private EditText email;
private EditText pwd;
private Button submit;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

email = (EditText)findViewById(R.id.editTextEmail);
pwd = (EditText)findViewById(R.id.editTextPassword);
submit = (Button)findViewById(R.id.buttonSubmit);

submit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

try{
Toast.makeText(getApplicationContext(), “test”, Toast.LENGTH_SHORT).show();
Map<String, String> map = new HashMap<String, String>();
String emailString = email.getText().toString();
String pwdString = pwd.getText().toString();
map.put(“email”, emailString);
map.put(“pwd”, pwdString);
boolean result = submitDataByHttpClientDoPost(map, “http://10.0.2.2/android_con.php&#8221;);

if(result) {
Toast.makeText(getApplicationContext(), “Data Inserted.”, Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(), “Failed”, Toast.LENGTH_SHORT).show();
}
}
catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
}
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

public Boolean submitDataByHttpClientDoPost(Map<String, String> map, String path) throws Exception {

HttpClient hc = new DefaultHttpClient();

HttpPost request = new HttpPost(path);

List<NameValuePair> parameters = new ArrayList<NameValuePair>();

for(Map.Entry<String, String> entry:map.entrySet()) {
NameValuePair nvp = new BasicNameValuePair(entry.getKey(), entry.getValue());
parameters.add(nvp);
}

HttpEntity entity = new UrlEncodedFormEntity(parameters, “UTF-8”);

request.setEntity(entity);

HttpResponse response = hc.execute(request);

if(response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
return true;
}
else {
return false;
}
}
}

 

main.xml

—————————————–

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical” >

<EditText
android:id=”@+id/editTextEmail”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:ems=”10″
android:hint=”Email” >

<requestFocus />
</EditText>

<EditText
android:id=”@+id/editTextPassword”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:ems=”10″
android:hint=”Password”
android:inputType=”textPassword” />

<Button
android:id=”@+id/buttonSubmit”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Submit” />

</LinearLayout>

 

android_con.php

——————————————

<?php

$con = mysql_connect(“localhost”, “root”, “password”);

if(!$con) {
die(‘Could not connect: ‘.mysql_connect);
}
else {

mysql_select_db(“database”, $con);

//get the android sending data
$str_email = $_REQUEST[“email”];
$str_pwd = $_REQUEST[“pwd”];
$sql = “insert into users(email, password) values(‘”.$str_email.”‘, ‘”.$str_pwd.”‘)”;

//insert data to database
if(!mysql_query($sql,$con)) {
return false;
}
else {
return true;
}

//close connection
mysql_close($con);

}

?>

 

Mainfest.xml

———————————–

<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.example.databasetest_1″
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”15″ />
<uses-permission android:name=”android.permission.INTERNET”/>

<application
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”.MainActivity”
android:label=”@string/title_activity_main” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>

 

 

Very Clear [ Android <=> Mysql ], no explain more, type again then u also can find out!

微博排名算法的研究

Posted: October 13, 2012 in Article

从新浪微博智能排序功能来看。影响一条微博的重要程度有以下几点因素:

1、微博发布时间。当前查看微博的时间与微博本身发布的时间的差可以知道一条微博的新鲜程度。这个因素对于微博时效性是非常重要的。

2、微博timeline的热度。timeline的热度也体现了微博内容本身的价值。热度高的timeline说明这条微博的受欢迎程度是很高的。热度的计算可以根据微博内容本身被转发次数,评论次数以及收藏次数来体现。

3、用户对微博的亲密度。亲密度是指用户A与用户B的亲密程度。例如,互粉情况下,亲密度可能会高一点等,用户A经常评论,转发或收藏用户B的微博,说明用户A与用户B交互性是比较强的。这个也是影响微博分数的一个因素。

4、用户对微博的兴趣度。用户的兴趣度,可以直接反映用户对微博内容的喜好程度。但兴趣度这个需要好好考量,因为用户本身可能也不知道自己感兴趣什 么内容。而现在通常的做法是,通过用户本身内容例如,用户标签,用户所关注的人的标签,以及用户关注的话题等等显示的内容分析出特定的“关键字标签”,这 些“关键字标签”可以作为用户感兴趣的“依据”。这样可以过滤一些与这些标签相匹配的微博内容。

不过,麦洛在这里不大建议收集微博内容,并从微博内容中提取“关键字标签”,因为微博内容是更新非常快,时效性很强。因此,历史的浏览记录并不能很 好的表达用户的兴趣。而且收集微博内容将使服务器压力很大,需要计算庞大的数据,时间越久,数据量越大,而且这些数据噪音很大。

从上面四点中可以知道,影响一条timeline分数的函数为

score=f(t,h,f,i);

其中t表示时间,h表示热度,f表示亲密度,i表示兴趣度。

接下来工作是为每一个因素寻找数学模型。例如,随时间变化的函数s1=f(t)可以使用《排名算法学习》http://miloisbadboy.com/archives/234中介绍的模型。

模型的选择需要根据实际情况和需求。

好了,模型的选择,可以好好看看排名算法的一些原理,可以从中获取一些灵感和启发。

 

 

文章转自: http://miloisbadboy.com/archives/237

As we know that Codeigniter provides a very good feature that can auto encrypt the session for the php application.


So for the higher security, we can put  random words here to instead of ‘session’ as the above pic showed. There, I will refer from other people’s tutorial, suggest a website that can generate the random key as showed below.

Here, you can copy the keys directly and put to the config at Codeigniter.

 

The website -> Random Key Generator

To remove the ‘index.php’, can config by the follwing steps:

1. create a new file in the root folder, and named -> ‘.htaccess’.

2. copy the following code to the ‘.htaccess’ file.

<IfModule mod_rewrite.c>
#http://codeigniter.com/wiki/mod_rewrite
RewriteEngine On
RewriteBase /

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#’system’ can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#When your application folder isn’t in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename ‘application’ to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn’t true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
# If we don’t have mod_rewrite installed, all 404’s
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin

ErrorDocument 404 /index.php
</IfModule>

3. change the ‘RewriteBase /’ if you put the codeigniter in other foler such as ‘/ci/’.
Then need to change as ‘RewriteBase /ci/’. (the sensitive of upper and lower case)

4. change the ‘application/config/config.php’
-> $config[‘index_page’] = ‘index.php’;
-> $config[‘index_page’] = ”;

5. open the Apache ‘httpd.conf’
-> find the line ‘#LoadModule rewrite_module modules/mod_rewrite.so’
-> delete the ‘#’ in front and save, start / restart Apache.

step 1 : install php

First, access http://php.iis.net/ , then download press “install PHP” button.

Then, click “install now”, download the file from the Website and open it.

Later, there will be a window shows your to install PHP.

And also there maybe need to download 3 files for installation.

Then accept, “next”. and after installation. You already build PHP on Windows.

======

step 2 : configure CGI

after that, maybe you want to test whether it can work.

But actually it didn’t finish yet.

You still need to follow 1 step which is :

Open the control panel -> find “Program” ->turn Windows Features on or not” -> “Internet Information Services” -> “World Wide Web Services” -> Application Development Features” -> and Select “CGI” ->Press “OK”

Then wait for a while……

======

step 3 : testing

Until now, you almost finish, so let’s have a test.

follow here.

Go to your “C:\inetpub\wwwroot” folder, and create a php file.

You can use Notepad, open Notepad and type:

 

<?php

phpinfor();

?>

 

Then save the file and named as “phpinfo.php” in the folder.

And to open your browser, and type “localhost/phpinfo.php” to address.

Enter….

If you can see the php version information, means the work has done.

—————-

Thanks