package com.cw.history; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; 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.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import android.app.Activity; import android.app.PendingIntent; import android.app.PendingIntent.CanceledException; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.util.Xml; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private String sj1 = convertStrToUnicode("粤S5W839|2013-01-01 00:00:00|2013-01-01 12:20:18"); private String path1 = "http://www.40006000.com/WebServiceYWT.asmx/DataHistoryAPI"; private String sj2 = "A00000321A199E|fc3244dd594a25cd6bb5c9cbf169fbaa"; private String path2 = "http://www.40006000.com/WebServiceYWT.asmx/GetBoundaryName"; private List<NameValuePair> params; class MyThread extends Thread { @Override public void run() { // TODO Auto-generated method stub MainActivity.this.params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("No", MainActivity.this.sj1)); // params.add(new BasicNameValuePair("para",MainActivity.this.sj2 // )); Log.i("out", MainActivity.this.sj1); post(MainActivity.this.path1, params); // post(MainActivity.this.path2, params); super.run(); } } // class MyThread1 extends Thread{ // @Override // public void run() { // // TODO Auto-generated method stub // // try { // get("http://www.40006000.com/WebServiceYWT.asmx/DataHistoryAPI?No="+URLEncoder.encode(MainActivity.this.sj1, // "UTF-8")); // } catch (UnsupportedEncodingException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // super.run(); // } // } // public static String get(String url){ // String response=null; // HttpClient httpClient=new DefaultHttpClient(); // HttpGet httpGet=new HttpGet(url); // try { // int // statusCode=httpClient.execute(httpGet).getStatusLine().getStatusCode(); // HttpResponse httpResponse=httpClient.execute(httpGet); // Log.i("out", ""+statusCode); // if(statusCode==HttpStatus.SC_OK){ // //获取返回结果 // response=EntityUtils.toString(httpResponse.getEntity()); // Log.i("out", response); // }else{ // response="请求错误,返回码:"+statusCode; // Log.i("out", response); // } // } catch (ClientProtocolException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (ParseException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } catch (IOException e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // return response; // } public static String post(String url, List<NameValuePair> params) { String logincode = ""; String response = null; HttpClient httpClient = new DefaultHttpClient(); // 创建post对象 HttpPost httpPost = new HttpPost(url); try { // 设置post请求参数,设置字符集 httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); // 使用execute方法发送post,并返回response对象 HttpResponse httpResponse = httpClient.execute(httpPost); int statusCode = httpResponse.getStatusLine().getStatusCode(); Log.i("out", "" + statusCode); if (statusCode == HttpStatus.SC_OK) { // 获取返回结果 response = EntityUtils.toString(httpResponse.getEntity()); Log.i("out", response); try { InputStream is=new ByteArrayInputStream(response.getBytes()); XmlPullParser parser = Xml.newPullParser(); parser.setInput(is, "utf-8"); int event = parser.getEventType(); //产生第一个事件 while(event!=XmlPullParser.END_DOCUMENT){ switch (event) { case XmlPullParser.START_DOCUMENT: break; case XmlPullParser.START_TAG: if("string".equals(parser.getName())){ logincode= parser.nextText(); Log.i("out", convertUnicodeToStr(logincode)); } break; case XmlPullParser.END_TAG: break; } event = parser.next(); } is.close(); } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { response = "请求错误,返回码:" + statusCode; Log.i("out", response); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return response; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt=(Button)this.findViewById(R.id.bt); bt.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "Gps", 1).show(); toggleGPS(); } }); new MyThread().start(); } /** * 开关gps */ private void toggleGPS() { Intent gpsIntent = new Intent(); gpsIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); gpsIntent.addCategory("android.intent.category.ALTERNATIVE"); gpsIntent.setData(Uri.parse("custom:3")); try { PendingIntent.getBroadcast(this, 0, gpsIntent, 0).send(); Log.i("opengps", "opengps"); } catch (CanceledException e) { e.printStackTrace(); System.out.print("这里报错!"); } } /** * 字符串和Unicode相互转换 */ /** * Unicode编码规则:Unicode码对每一个字符用4位16进制数表示。 * 具体规则是:将一个字符(char)的高8位与低8位分别取出,转化为16进制数, * 如果转化的16进制数的长度不足2位,则在高位补0,然后将高、低8位转成的16进制字符串拼接起来并在前面补上"\\u" 即可。 * * @param str * @return */ public static String convertStrToUnicode(String str) { if (str == null || str.length() == 0) { return ""; } StringBuffer sb = new StringBuffer(1000); // 临时变量 int hexNumber; char c; String hexStr; for (int i = 0; i < str.length(); i++) { // 加上\\u前缀 sb.append("\\u"); // 取出第i为的char字符 c = str.charAt(i); // 取出高8位 hexNumber = (c >>> 8); hexStr = Integer.toHexString(hexNumber); if (hexStr.length() == 1) sb.append("0"); sb.append(hexStr); // 取出低8位 hexNumber = (c & 0xFF); hexStr = Integer.toHexString(hexNumber); if (hexStr.length() == 1) sb.append("0"); sb.append(hexStr); } return sb.toString(); } /** * 将Unicode编码的字符串转换为String * * @param unicodeStr * @return */ public static String convertUnicodeToStr(String unicodeStr) { if (unicodeStr == null || unicodeStr.length() == 0) { return ""; } String unicodePrefix = "\\u"; String tempStr; String tempHexStr; StringBuffer sb = new StringBuffer(1000); while (unicodeStr.contains(unicodePrefix)) { // 获取第一次出现\\u的index int firstIndex = unicodeStr.indexOf(unicodePrefix); // 获取第二次出现\\u的index int secondIndex = unicodeStr.indexOf(unicodePrefix, firstIndex + 2); // 将第一出现与第二次出现中间的部分,截取下来 if (secondIndex == -1) { tempStr = unicodeStr.substring(firstIndex); } else { tempStr = unicodeStr.substring(firstIndex, secondIndex); } tempHexStr = tempStr.substring(tempStr.indexOf(unicodePrefix) + 2); if (tempHexStr.length() == 4) { sb.append((char) Integer.parseInt(tempHexStr, 16)); } // 将第二次出现以后的部分截取下来 if (secondIndex == -1) { unicodeStr = ""; } else { unicodeStr = unicodeStr.substring(secondIndex); } } return sb.toString(); } /** * 字符串和ASCII相互转换 */ // public static String stringToAscii(String str) {// 字符串转换为ASCII码 // // char[] chars = str.toCharArray(); // 把字符中转换为字符数组 // // String s = ""; // // System.out.println("\n\n汉字 ASCII\n----------------------"); // for (int i = 0; i < chars.length; i++) {// 输出结果 // // System.out.println(" " + chars[i] + " " + (int) chars[i]); // // s += (int) chars[i] + ","; // } // // return s; // } // // public static String asciiToString(String str) {// ASCII转换为字符串 // // String[] chars = str.split(","); // String s=""; // System.out.println("ASCII 汉字 \n----------------------"); // for (int i = 0; i < chars.length; i++) { // String c=(char) Integer.parseInt(chars[i])+""; // System.out.println(chars[i] + " "+ c); // s += c; // } // // return s; // } }