編輯:關於Android編程
下面用兩種方法
第一種:
是類似於直接請求訪問url
代碼如下:
[java]
package com.jouhu.util;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class AssignService {
public static String getServiceName(String assignUrl){
String name=null;
try {
URL url = new URL(assignUrl);
URLConnection uc = url.openConnection();
uc.setConnectTimeout(5000);
uc.setReadTimeout(8000);
InputStream in = uc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
name = br.readLine();
br.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return name;
}
public static void main(String[] arg) throws InterruptedException{
final String URL_ASSIGN = "http://192.168.1.192:8888/ServiceAssign/assignService.action";
int num=500;
final CountDownLatch begin = new CountDownLatch(1);
final CountDownLatch end = new CountDownLatch(num);
final ExecutorService exec = Executors.newFixedThreadPool(num);
for (int index = 0; index < num; index++) {
Runnable run = new Runnable() {
public void run() {
try {
begin.await();
AssignService.getServiceName(URL_ASSIGN);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
end.countDown();
}
}
};
exec.submit(run);
}
System.out.println("Start");
begin.countDown();
end.await();
exec.shutdown();
System.out.println("Over");
}
}
第二種:
通過POST將本地數據發送給服務器,string參數表
[java]
/**
* GpsPoiService.java
* 版權所有(C) 2012
* 創建:cuiran 2012-9-12 下午2:49:25
*/
package com.jouhu.service;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import android.util.Log;
import com.jouhu.util.AssignService;
import com.jouhu.util.BaseNameValuePair;
import com.jouhu.util.Conf;
/**
* TODO
* @author cuiran
* @version TODO
*/
public class GpsPoiService {
private String url="http://219.143.94.189:8405/fastweb/fastweb.do?m=addgps";//服務地址
public String sendMsg(double x,double y,String address) {
//URLEncoder.encode(address, "gbk")
String re="";
try{
List<NameValuePair> datas = new ArrayList<NameValuePair>();
datas.add(new BaseNameValuePair("x", ""+x));
datas.add(new BaseNameValuePair("y", ""+y));
datas.add(new BaseNameValuePair("address", ""+address));
re=sendDataByPost(url, datas);
// StringBuffer str = new StringBuffer();
// str.append(url)
// .append("&x=")
// .append(x)
// .append("&y=")
// .append(y)
// .append("&address=")
// .append(URLEncoder.encode(address, "gbk"));
// Log.i(Conf.TAG, str.toString());
// re = AssignService.getServiceName(str.toString());
}catch(Exception e){
Log.i(Conf.TAG, "-------"+e.getLocalizedMessage());
}
Log.i(Conf.TAG, re);
return re;
}
// 通過POST將本地數據發送給服務器,string參數表
public static String sendDataByPost(String url, List<NameValuePair> datas){
org.apache.http.client.HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
HttpResponse resp = null;
String result = "";
// post data
try {
post.setEntity(new UrlEncodedFormEntity(datas,HTTP.UTF_8));
resp = client.execute(post);
result = EntityUtils.toString(resp.getEntity());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
使用第二種常用的編碼是UTF-8,所以後台服務器也需要用該編碼接收參數。
之所以把PopupWindow與懸浮窗這兩個放到一起講,是因為這兩個的實現原理基本是一致的,只是有點不同而已。 原理: 使用系統服務(WindowManag
這裡我們使用9patch制作一個背景,找到SDK目錄下的tools目錄,雙擊draw9patch.bat文件,如下圖: @drawable/backgr
以前給大家介紹的xUtils是國內比較火的快速開發框架,但是它的注解機制不是太穩定而且注解可選也比較少,今天給大家介紹一個國外的一個框架主要專注於注解的開發,簡化Andr
引言:去年Android 6.0發布後,其新引入的(Requesting Permissions at Run Time)運行時權限就備受開發者關注,隨著今年國內手機廠商