The following two methods are used:
First:
Is similar to a direct request url
The Code is as follows:
[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 ");
}
}
Second:
Send local data to the server through POST, string parameter table
[Java]
/**
* GpsPoiService. java
* Copyright (C) 2012
* Creation: cuiran 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. do? M = addgps "; // service address
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;
}
// Send local data to the server through POST, string parameter table
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.exe cute (post );
Result = EntityUtils. toString (resp. getEntity ());
} Catch (UnsupportedEncodingException e ){
E. printStackTrace ();
} Catch (ClientProtocolException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
Return result;
}
}
The second commonly used encoding is the UTF-8, so the backend server also needs to use this encoding to receive parameters.