«

»

制作省份城市选择列表

快抢沙发

利用dom技术将省份和城市列表项逐一输出,并且假设设置的有默认省份和城市,能直接选中。
2016-06-29_195859

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>城市列表</title>
	<script src="CitysData.js" ></script>

</head>
<body>
<script>

function cc(sf, cs){
	document.write('<select onchange="select(this)" id="provinces">');
	document.write('<option value="">--选择省份--</option>');
	var a=0;
	for(var i=0;i<arrCity.length;i++){
		if(typeof(sf)!="undefined"){
			if(arrCity[i]["name"] == sf){
				var sel ="selected";
				a = i;
			}else{
				var sel="";
			}

		}
		
		document.write('<option ' + sel +' value="'+arrCity[i]["name"]+'">'+arrCity[i]["name"]+'</option>');
	}
	document.write("</select>");

	document.write('<select id="citys" name="+cname+">');
	document.write('<option value="">--选择城市--</option>');
	document.write("</select>");
	if(a >0){
		select(document.getElementById("provinces"), cs, a);
	}
}

function select(proobj, cs ,a){
	
	if(typeof(a)!="undefined"){
		var index =a;
	}else{
		var index = proobj.selectedIndex-1;
	}


	var citys = document.getElementById("citys");
	var sub = arrCity[index]["sub"];
	citys.innerHTML="";
	for(var i=0;i<sub.length;i++){

		if(sub[i]["name"] == cs ){
			var sel ="selected";
		}else{
			var sel="";
		}
		var option =document.createElement("option");
		var text=sub[i]["name"];
		//console.log(text);
		option.value=text;
		option.selected=sel;
		option.innerHTML=text;
		citys.appendChild(option);
	}
}

//用户默认的省份是湖北,城市是武汉
cc("湖北", "武汉");



</script>


	
</body>
</html>

里面的函数“cc”是显示省份,函数“select”是根据用户选定的省份,响应的显示该省管辖的城市。
上面引用的CitysData.js文件里面包含的中国所有的省份名和城市名,用的是百度的数据,地址是城市数据地址,可能部分需要更新。

2016.06.29
H
阅读全文...

PHP版Google People API的坑

快抢沙发

最近在学习Google People API,先看的PHP版的,当GET https://people.googleapis.com/v1/people/me 的时候,想指定获取的内容,比如person.names,person.phoneNumbers,谷歌提供的接口是传入一个数组Query parameters,
Parameter name Type Description
pageToken string The token of the page to be returned.
pageSize number The number of connections to include in the response. Valid values are between 1 and 500, inclusive. Defaults to 100.
sortOrder enum(SortOrder) The order in which the connections should be sorted. Defaults to LAST_MODIFIED_ASCENDING.
syncToken string A sync token, returned by a previous call to people.connections.list. Only resources changed since the sync token was created are returned.
requestMask object(RequestMask) A mask to restrict results to a subset of person fields.

2016-06-25_190016
可以看到最后一个参数requestMask需要传入的值是object,一个对象。
继续点开RequestMask看
2016-06-25_190532
参数includeField要传入的值是string内容
看到这些大家会怎么传入参数呢,于是很自然的想到创建一个RequestMask类,这个RequestMask类有一个参数includeField,将“person.names,person.phoneNumbers”赋值给includeField,最后再将RequestMask传入Query parameters,结果是,我失败了,在网上找答案,没有直接找到,最后无意中还是发现了解决办法
2016-06-25_192504
无语了吧,

$connections = $service->people_connections->listPeopleConnections('people/me', array(
    'pageSize' => 500,
    'requestMask.includeField' => 'person.names,person.phoneNumbers'
));

想多了,什么传入类不类的,压根都不需要!直接将person.names,person.phoneNumbers赋值给requestMask.includeField即可,无语了吧。直接说string类型不就行了?requestMask.includeField?说好的object呢?难道是string这个object啊

2016.06.25
H
阅读全文...

eclipse报错”main” java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest

快抢沙发

今天学习了下谷歌的People API中的java版,具体步骤可以见,在这里第一次用了下gradle工具,用着还行。
用eclipse运行Quickstart.java
源码如下:



import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.people.v1.People;
import com.google.api.services.people.v1.PeopleScopes;
import com.google.api.services.people.v1.model.ListConnectionsResponse;
import com.google.api.services.people.v1.model.Name;
import com.google.api.services.people.v1.model.Person;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

public class PeopleQuickstart {
    /** Application name. */
    private static final String APPLICATION_NAME =
        "People API Java Quickstart";

    /** Directory to store user credentials for this application. */
    private static final java.io.File DATA_STORE_DIR = new java.io.File(
        System.getProperty("user.home"), ".credentials/people.googleapis.com-java-quickstart.json");

    /** Global instance of the {@link FileDataStoreFactory}. */
    private static FileDataStoreFactory DATA_STORE_FACTORY;

    /** Global instance of the JSON factory. */
    private static final JsonFactory JSON_FACTORY =
        JacksonFactory.getDefaultInstance();

    /** Global instance of the HTTP transport. */
    private static HttpTransport HTTP_TRANSPORT;

    /** Global instance of the scopes required by this quickstart.
     *
     * If modifying these scopes, delete your previously saved credentials
     * at ~/.credentials/people.googleapis.com-java-quickstart.json
     */
    private static final List<String> SCOPES =
        Arrays.asList(PeopleScopes.CONTACTS_READONLY);

    static {
        try {
            HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
            DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
        } catch (Throwable t) {
            t.printStackTrace();
            System.exit(1);
        }
    }

    /**
     * Creates an authorized Credential object.
     * @return an authorized Credential object.
     * @throws IOException
     */
    public static Credential authorize() throws IOException {
        // Load client secrets.
        InputStream in =
            PeopleQuickstart.class.getResourceAsStream("/client_secret.json");
        GoogleClientSecrets clientSecrets =
            GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        // Build flow and trigger user authorization request.
        GoogleAuthorizationCodeFlow flow =
                new GoogleAuthorizationCodeFlow.Builder(
                        HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                .setDataStoreFactory(DATA_STORE_FACTORY)
                .setAccessType("offline")
                .build();
//        Credential credential = new AuthorizationCodeInstalledApp(
//            flow, new LocalServerReceiver()).authorize("user");
        Credential credential = new AuthorizationCodeInstalledApp(
        flow, new LocalServerReceiver.Builder().setPort(54771).build()).authorize("user");
        System.out.println(
                "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
        return credential;
    }

    /**
     * Build and return an authorized People client service.
     * @return an authorized People client service
     * @throws IOException
     */
    public static People getPeopleService() throws IOException {
        Credential credential = authorize();
        return new People.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                .setApplicationName(APPLICATION_NAME)
                .build();
    }

    public static void main(String [] args) throws IOException {
        People service = getPeopleService();

        // Request 10 connections.
        ListConnectionsResponse response = service.people().connections()
                .list("people/me")
                .setPageSize(10)
                .execute();

        // Print display name of connections if available.
        List<Person> connections = response.getConnections();
        if (connections != null && connections.size() > 0) {
            for (Person person : connections) {
                List<Name> names = person.getNames();
                if (names != null && names.size() > 0) {
                    System.out.println("Name: " + person.getNames().get(0)
                            .getDisplayName());
                } else {
                    System.out.println("No names available for connection.");
                }
            }
        } else {
            System.out.println("No connections found.");
        }
    }

}

其中的81~82行,我改用了83~84行,因为LocalServerReceiver默认会用一个未被占用的随机的端口,经过改用后指定54711端口,那样才好在Google console里面的Credentials添加固定的Authorized redirect URIs,不用来回变了。
在代码运行时发现始终未弹出授权页面,看看了eclipse的控制台,发现错误信息如下:
六月 25, 2016 2:05:52 下午 com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly
警告: unable to change permissions for everybody: E:\.credentials\people.googleapis.com-java-quickstart.json
六月 25, 2016 2:05:52 下午 com.google.api.client.util.store.FileDataStoreFactory setPermissionsToOwnerOnly
警告: unable to change permissions for owner: E:\.credentials\people.googleapis.com-java-quickstart.json
Exception in thread “main” java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
at com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver.getRedirectUri(LocalServerReceiver.java:98)
at com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.authorize(AuthorizationCodeInstalledApp.java:76)
at PeopleQuickstart.authorize(PeopleQuickstart.java:82)
at PeopleQuickstart.getPeopleService(PeopleQuickstart.java:94)
at PeopleQuickstart.main(PeopleQuickstart.java:101)
Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServletRequest
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
… 5 more

细看了一下,Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServletRequest,原来是该没有将tomcat的servlet-api.jar放到项目的JRE System Library下,放入后立刻搞定!

2016.06.25
H
阅读全文...

javascript让图片来回移动

快抢沙发

本示例中用了四张图片,分别是四个方向,用它们来实现图片向前滚动的效果。
图片滚动

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>图片移动</title>
</head>
<body>	
	<img id ="ren" border ="0" style="position:absolute;left:10px;top:150px;width=180px" src="favicon1.png">

	<script>
		//获取图片名称
		function basename(url){
			var loc = url.lastIndexOf("/") + 1;
			return url.substr(loc);
			
		}
		
		//创建图片对象
		var ren = document.getElementById("ren");

		function changetu(){
			var ind = basename(ren.src).charAt(7);
			if( ind == "1"){
				ren.src="favicon" + "2" + ".png" ;
			}else if(ind == "2"){
				ren.src="favicon" + "3" + ".png" ;
			}else if(ind =="3"){
				ren.src="favicon" + "4" + ".png" ;
			}else{
				ren.src="favicon" + "1" + ".png" ;
			}
		}

		

		var x=0; x向前进距离
		var xs=20; //x向前进加速度

		function run(){
			var winWidth=0;
			if (window.innerWidth){
				winWidth = window.innerWidth;
			}else if ((document.body) && (document.body.clientWidth)){
				winWidth = document.body.clientWidth;
			}
			
			x +=xs;
			if( x>= winWidth - parseInt(ren.width) -13 || x<=0){ //用parseInt可以让获取的值不含有"px";-13是用来修正

				xs = (-1)*xs;
			}
			ren.style.left = x +"px";
		}

		setInterval(function(){
			changetu();
			run();
		},500)


	</script>
</body>
</html>

看看代码实现起来其实很简单。需要稍微提一下的是:

if( x>= winWidth - parseInt(ren.width) -13 || x<=0){ //用parseInt可以让获取的值不含有"px";-13是用来修正

				xs = (-1)*xs;
			}

利用 xs= (-1)*xs 实现图片走到最右边后转向回走的功能

2016.06.20
H
阅读全文...
  • sitemap_baidu