Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4315
  • 博文数量: 1
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2015-01-13 13:40
文章分类
文章存档

2015年(1)

我的朋友
最近访客

分类: Android平台

2015-01-13 16:56:55

    之前的servlet文章中熟悉了下servlet的知识,练习了下;这里开始andriod和servlet服务通信了,昨天晚上一点多才调通,人生就是一出杯具,我喜欢喝的确是蜂蜜,嘿嘿!!!

    讲之前先建立一个java工程,这个工程目的是测试之前的servlet【 http://blog.chinaunix.net/uid-25799257-id-3774015.html】 的doPost方法,直接上吧,召唤赤兔马 汗  
    HttpServlet/(习惯linux下的左斜杠了)HttpUtils.java

点击(此处)折叠或打开

  1. import java.io.ByteArrayOutputStream;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.net.HttpURLConnection;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.net.URLEncoder;
  9. import java.util.HashMap;
  10. import java.util.Map;

  11. @SuppressWarnings("unused")
  12. public class HttpUtils {
  13.     ///< 请求服务URL
  14.     private final static String PATH = "";
  15.     private static URL url;
  16.     
  17.     public HttpUtils() {
  18.         // TODO Auto-generated constructor stub
  19.     }
  20.     
  21.     /**
  22.      * 向服务端提交数据
  23.      * @param params    url参数
  24.      * @param encode    字节编码
  25.      * @return
  26.      */
  27.     @SuppressWarnings("deprecation")
  28.     public static int sendMessage(Map<String, String> params, String encode){
  29.         ///< 初始化URL
  30.         StringBuffer buffer = new StringBuffer();
  31. //        StringBuffer buffer = new StringBuffer();
  32.         
  33.         if (null != params && !params.isEmpty()){
  34.             for (Map.Entry<String, String> entry : params.entrySet()){
  35.                 buffer.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue())).append("&");
  36.             }
  37.             ///< 删除多余的&
  38.             buffer.deleteCharAt(buffer.length() - 1);
  39.         }
  40.         
  41.         ///< show url
  42.         System.out.println(buffer.toString());
  43.         
  44.         try {
  45.             url = new URL(PATH);
  46.             if (null != url)
  47.             {
  48.                 HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
  49.                 if (null == urlConnection)
  50.                 {
  51.                     return -1;
  52.                 }
  53.                 urlConnection.setConnectTimeout(3000);
  54.                 urlConnection.setRequestMethod("POST");    ///< 设置请求方式
  55.                 urlConnection.setDoInput(true);            ///< 表示从服务器获取数据
  56.                 urlConnection.setDoOutput(true);        ///< 表示向服务器发送数据

  57.                 byte[] data = buffer.toString().getBytes();
  58.                 ///< 设置请求体的是文本类型
  59.                 urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  60.                 urlConnection.setRequestProperty("Content-Length", String.valueOf(data.length));
  61.                 ///< 获得输出流
  62.                 OutputStream outputStream = urlConnection.getOutputStream();
  63.                 outputStream.write(data);
  64.                 outputStream.close();
  65.                 ///< 获得服务器的响应结果和状态码
  66.                 int responseCode = urlConnection.getResponseCode();
  67.                 System.out.println("" + responseCode);
  68.                 if (200 == responseCode)
  69.                 {
  70.                     return changeInputStream(urlConnection.getInputStream(), encode);
  71.                 }
  72.             }
  73.         } catch (IOException e) {
  74.             // TODO Auto-generated catch block
  75.             e.printStackTrace();
  76.         }
  77.         
  78.         return -1;
  79.     }
  80.     
  81.     /**
  82.      * 获得网络返回值【0 - 正确    1 - 用户名错误    2 - 密码错误】
  83.      * @param inputStream
  84.      * @param encode
  85.      * @return
  86.      */
  87.     private static int changeInputStream(InputStream inputStream, String encode) {
  88.         // TODO Auto-generated method stub
  89.         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  90.         byte[] data = new byte[1024];
  91.         int len = 0;
  92.         String result = "";
  93.         if (null != inputStream)
  94.         {
  95.             try {
  96.                 while ((len = inputStream.read(data)) != -1)
  97.                 {
  98.                     outputStream.write(data, 0, len);
  99.                 }
  100.                 result = new String(outputStream.toByteArray(), encode);
  101.                 System.out.println(result);
  102.                 
  103.                 len = Integer.parseInt(result.substring(0, 1));
  104.                 
  105.             } catch (IOException e) {
  106.                 // TODO Auto-generated catch block
  107.                 e.printStackTrace();
  108.             }
  109.         }
  110.         return len;
  111.     }
  112.     
  113.     public static void main(String[] args) {
  114.         ///< 发送用户名和密码到服务器进行校验,并获得服务器返回值
  115.         Map<String, String> params = new HashMap<String, String>();
  116.         params.put("user_name", "admin");
  117.         params.put("password", "123");
  118.         String encode = "utf-8";
  119.         
  120.         int result = HttpUtils.sendMessage(params, encode);
  121.         System.out.println("" + result);
  122.     }
  123. }
    运行java程序【之前记得启动tomcat小猫】,ok!
    
    这里的java程序就可以直接作为一个http访问的工具类进行扩展,之后就可以将其移植到andriod程序中作为一个工具类,挺不错的,我觉得.【说明一点:移植后在android模拟器里运行应用时访问的127.0.0.1是模拟器自己的,而不是我们的本机的,因此改为10.0.2.2就可以了
    

    android工程就两个文件----->
    HttpUtils.java

点击(此处)折叠或打开

  1. package com.android.taxi;

  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.net.HttpURLConnection;
  7. import java.net.MalformedURLException;
  8. import java.net.URL;
  9. import java.net.URLEncoder;
  10. import java.util.HashMap;
  11. import java.util.Map;

  12. import org.apache.http.HttpResponse;

  13. @SuppressWarnings("unused")
  14. public class HttpUtils {
  15.     ///< 请求服务URL
  16.         private final static String PATH = "";
  17.         private static URL url;
  18.         
  19.         public HttpUtils() {
  20.             // TOiDO Auto-generated constructor stub
  21.         }
  22.         
  23.         /**
  24.          * 向服务端提交数据
  25.          * @param params    url参数
  26.          * @param encode    字节编码
  27.          * @return
  28.          */
  29.         @SuppressWarnings("deprecation")
  30.         public static int sendMessage(Map<String, String> params, String encode){
  31.             ///< 初始化URL
  32.             StringBuffer buffer = new StringBuffer();
  33. //            StringBuffer buffer = new StringBuffer();
  34.             
  35.             if (null != params && !params.isEmpty()){
  36.                 for (Map.Entry<String, String> entry : params.entrySet()){
  37.                     buffer.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue())).append("&");
  38.                 }
  39.                 ///< 删除多余的&
  40.                 buffer.deleteCharAt(buffer.length() - 1);
  41.             }
  42.             
  43.             ///< show url
  44.             //System.out.println(buffer.toString());
  45.             
  46.             try {
  47.                 url = new URL(PATH);
  48.                 if (null != url)
  49.                 {
  50.                     HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
  51.                     if (null == urlConnection)
  52.                     {
  53.                         return -1;
  54.                     }
  55.                     urlConnection.setConnectTimeout(3000);
  56.                     urlConnection.setRequestMethod("POST");    ///< 设置请求方式
  57.                     urlConnection.setDoInput(true);            ///< 表示从服务器获取数据
  58.                     urlConnection.setDoOutput(true);        ///< 表示向服务器发送数据

  59.                     byte[] data = buffer.toString().getBytes();
  60.                     ///< 设置请求体的是文本类型
  61.                     urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  62.                     urlConnection.setRequestProperty("Content-Length", String.valueOf(data.length));
  63.                     ///< 获得输出流
  64.                     OutputStream outputStream = urlConnection.getOutputStream();
  65.                     outputStream.write(data);
  66.                     outputStream.close();
  67.                     ///< 获得服务器的响应结果和状态码
  68.                     int responseCode = urlConnection.getResponseCode();
  69.                     //System.out.println("" + responseCode);
  70.                     if (200 == responseCode)
  71.                     {
  72.                         return changeInputStream(urlConnection.getInputStream(), encode);
  73.                     }
  74.                 }
  75.             } catch (IOException e) {
  76.                 // TODO Auto-generated catch block
  77.                 e.printStackTrace();
  78.             }
  79.             
  80.             return -1;
  81.         }
  82.         
  83.         /**
  84.          * 获得网络返回值【0 - 正确    1 - 用户名错误    2 - 密码错误】
  85.          * @param inputStream
  86.          * @param encode
  87.          * @return
  88.          */
  89.         private static int changeInputStream(InputStream inputStream, String encode) {
  90.             // TODO Auto-generated method stub
  91.             ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  92.             byte[] data = new byte[1024];
  93.             int len = 0;
  94.             String result = "";
  95.             if (null != inputStream)
  96.             {
  97.                 try {
  98.                     while ((len = inputStream.read(data)) != -1)
  99.                     {
  100.                         outputStream.write(data, 0, len);
  101.                     }
  102.                     result = new String(outputStream.toByteArray(), encode);
  103.                     //System.out.println(result);
  104.                     
  105.                     len = Integer.parseInt(result.substring(0, 1));
  106.                     
  107.                 } catch (IOException e) {
  108.                     // TODO Auto-generated catch block
  109.                     e.printStackTrace();
  110.                 }
  111.             }
  112.             return len;
  113.         }
  114. }


    LoginActivity.java

点击(此处)折叠或打开

  1. package com.android.taxi;

  2. //import net.youmi.android.AdManager;
  3. //import net.youmi.android.banner.AdSize;
  4. //import net.youmi.android.banner.AdView;

  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import com.android.taxi.R;
  8. import android.os.Bundle;
  9. import android.os.Handler;
  10. import android.os.Looper;
  11. import android.os.Message;
  12. import android.annotation.SuppressLint;
  13. import android.app.Activity;
  14. import android.view.Menu;
  15. import android.view.View;
  16. //import android.view.ViewGroup.LayoutParams;
  17. import android.widget.Button;
  18. import android.widget.EditText;
  19. import android.widget.LinearLayout;
  20. import android.widget.Toast;

  21. @SuppressLint("HandlerLeak")
  22. public class LoginActivity extends Activity {

  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         
  27.         // e2d468f618be14b5和4955a15c0ddb7c5b位置填写你的发布ID和应用密钥
  28.         //AdManager.init(this,"e2d468f618be14b5", "4955a15c0ddb7c5b", 30, false);
  29.         
  30.         setContentView(R.layout.activity_login);
  31.         
  32.         ///< 初始化Layout控件,便于加载广告视图
  33.         //layout = new LinearLayout(LoginActivity.this);
  34.         //layout.setOrientation(LinearLayout.VERTICAL);

  35.         ///< 初始化广告视图
  36.         //AdView adView = new AdView(LoginActivity.this, AdSize.SIZE_320x50);
  37.         //LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
  38.                 //LayoutParams.WRAP_CONTENT);
  39.         //layout.addView(adView, params);

  40.         findViews();
  41.         setListeners();
  42.     }
  43.     
  44.     public void findViews()
  45.     {
  46.         button_login = (Button)findViewById(R.id.login);
  47.         user_name = (EditText)findViewById(R.id.user_name);
  48.         password = (EditText)findViewById(R.id.password);
  49.     }
  50.     
  51.     public void setListeners()
  52.     {
  53.         button_login.setOnClickListener(loginTest);
  54.     }
  55.     
  56.     private Button.OnClickListener loginTest = new Button.OnClickListener()
  57.     {
  58.         @SuppressLint("NewApi")
  59.         @Override
  60.         public void onClick(View arg0) {
  61.             // TODO Auto-generated method stub
  62.             String userName = null;
  63.             String passWord = null;
  64.             
  65.             ///< 简单判断用户是否输入用户名,是否输入密码
  66.             if ((userName = user_name.getText().toString()).isEmpty())
  67.             {
  68.                 if (password.getText().toString().isEmpty())
  69.                 {
  70.                     Toast.makeText(LoginActivity.this, "请输入用户名和密码", Toast.LENGTH_SHORT).show();
  71.                     return;
  72.                 }
  73.                 Toast.makeText(LoginActivity.this, "请输入用户名!", Toast.LENGTH_SHORT).show();
  74.                 return;
  75.             }
  76.             else if ((passWord = password.getText().toString()).isEmpty())
  77.             {
  78.                 Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
  79.                 return;
  80.             }
  81.             
  82.             loginHandle(userName, passWord);
  83.             
  84.             //            ///< 发送用户名和密码到服务器进行校验,并获得服务器返回值
  85.             //            Map params = new HashMap();
  86.             //            params.put("user_name", userName);
  87.             //            params.put("password", passWord);
  88.             //            String encode = "utf-8";
  89.             //            
  90.             //            //            Toast.makeText(LoginActivity.this, params.get("user_name"), Toast.LENGTH_SHORT).show();
  91.             //            //            Toast.makeText(LoginActivity.this, params.get("password"), Toast.LENGTH_SHORT).show();
  92.             //
  93.             //            int responseCode = HttpUtils.sendMessage(params, encode);
  94.             //            if (0 == responseCode)
  95.             //            {
  96.             //                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
  97.             //            }
  98.             //            else if (1 == responseCode)
  99.             //            {
  100.             //                Toast.makeText(LoginActivity.this, "用户名错误!", Toast.LENGTH_SHORT).show();
  101.             //            }
  102.             //            else if (2 == responseCode)
  103.             //            {
  104.             //                Toast.makeText(LoginActivity.this, "密码错误!", Toast.LENGTH_SHORT).show();
  105.             //            }
  106.             //            else
  107.             //            {
  108.             //                Toast.makeText(LoginActivity.this, "异常!", Toast.LENGTH_SHORT).show();
  109.             //            }
  110.                         
  111.             //            ///< 发送用户名和密码到服务器进行校验,并获得服务器返回值
  112.             //            // TODO
  113.             //            if (userName.equals("admin") && passWord.equals("123"))
  114.             //            {
  115.             //                Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
  116.             //            }
  117.             //            else
  118.             //            {
  119.             //                Toast.makeText(LoginActivity.this, "请核对用户信息!", Toast.LENGTH_SHORT).show();
  120.             //            }
  121.         }

  122.     };
  123.     
  124.     public void loginHandle(final String userName, final String password)
  125.     {
  126.         new Thread(new Runnable()
  127.         {
  128.             @Override
  129.             public void run()
  130.             {
  131.                 ///< 发送用户名和密码到服务器进行校验,并获得服务器返回值
  132.                 Map<String, String> params = new HashMap<String, String>();
  133.                 params.put("user_name", userName);
  134.                 params.put("password", password);
  135.                 String encode = "utf-8";
  136.                 
  137.                 //            Toast.makeText(LoginActivity.this, params.get("user_name"), Toast.LENGTH_SHORT).show();
  138.                 //            Toast.makeText(LoginActivity.this, params.get("password"), Toast.LENGTH_SHORT).show();

  139.                 int responseCode = HttpUtils.sendMessage(params, encode);
  140.                 Looper.prepare();
  141.                 if (0 == responseCode)
  142.                 {
  143.                     Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
  144.                 }
  145.                 else if (1 == responseCode)
  146.                 {
  147.                     Toast.makeText(LoginActivity.this, "用户名错误!", Toast.LENGTH_SHORT).show();
  148.                 }
  149.                 else if (2 == responseCode)
  150.                 {
  151.                     Toast.makeText(LoginActivity.this, "密码错误!", Toast.LENGTH_SHORT).show();
  152.                 }
  153.                 else
  154.                 {
  155.                     Toast.makeText(LoginActivity.this, "异常!", Toast.LENGTH_SHORT).show();
  156.                 }
  157.                 Looper.loop();
  158.                 
  159.                 //执行完毕后给handler发送一个空消息
  160.                 handler.sendEmptyMessage(0);
  161.             }
  162.         }).start();
  163.     }

  164.     @Override
  165.     public boolean onCreateOptionsMenu(Menu menu) {
  166.         // Inflate the menu; this adds items to the action bar if it is present.
  167.         getMenuInflater().inflate(R.menu.login, menu);
  168.         return true;
  169.     }
  170.     
  171.     //定义Handler对象
  172.     private Handler handler =new Handler(){
  173.         @Override
  174.         //当有消息发送出来的时候就执行Handler的这个方法
  175.         public void handleMessage(Message msg){
  176.             super.handleMessage(msg);
  177.             // TODO 处理UI
  178.         }
  179.     };
  180.         
  181.     private Button button_login;
  182.     private EditText user_name;
  183.     private EditText password;
  184.     
  185.     LinearLayout layout;
  186. }
    布局文件:activity_login.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android=""
  3.     android:layout_width="fill_parent"
  4.     android:layout_height="wrap_content"
  5.     android:orientation="vertical" >

  6.     <!-- android:layout_weight="10" 失效了【最外层LinearLayout的布局高度是内容包裹】 -->
  7.     <LinearLayout
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="wrap_content"
  10.         android:layout_marginBottom="15dp"
  11.         android:layout_marginTop="10dp"
  12.         android:layout_weight="10"
  13.         android:orientation="horizontal" >

  14.         <TextView
  15.             android:layout_width="wrap_content"
  16.             android:layout_height="wrap_content"
  17.             android:text="@string/user_name"
  18.             android:textSize="18sp" />

  19.         <EditText
  20.             android:id="@+id/user_name"
  21.             android:layout_width="fill_parent"
  22.             android:layout_height="wrap_content"
  23.             android:layout_marginLeft="5dp"
  24.             android:layout_marginRight="5dp"
  25.             android:background="@drawable/edit_text"
  26.             android:inputType="textPersonName"
  27.             android:padding="7dp"
  28.             android:text="" />
  29.     </LinearLayout>

  30.     <!-- android:layout_weight="2" 失效了 【最外层LinearLayout的布局高度是内容包裹】 -->
  31.     <LinearLayout
  32.         android:layout_width="fill_parent"
  33.         android:layout_height="wrap_content"
  34.         android:layout_marginBottom="15dp"
  35.         android:layout_weight="2"
  36.         android:orientation="horizontal" >

  37.         <TextView
  38.             android:layout_width="wrap_content"
  39.             android:layout_height="wrap_content"
  40.             android:text="@string/password"
  41.             android:textSize="18sp" />

  42.         <EditText
  43.             android:id="@+id/password"
  44.             android:layout_width="fill_parent"
  45.             android:layout_height="wrap_content"
  46.             android:layout_marginLeft="5dp"
  47.             android:layout_marginRight="5dp"
  48.             android:background="@drawable/edit_text"
  49.             android:inputType="textPassword"
  50.             android:padding="7dp"
  51.             android:text="" />
  52.     </LinearLayout>

  53.     <Button
  54.         android:id="@+id/login"
  55.         android:layout_width="fill_parent"
  56.         android:layout_height="wrap_content"
  57.         android:layout_marginLeft="59dp"
  58.         android:layout_marginRight="5dp"
  59.         android:background="@drawable/buton_style"
  60.         android:text="@string/login_btn" />

  61. </LinearLayout>
      strings.xml

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>

  3.     <string name="app_name">TAXI</string>
  4.     <string name="action_settings">Settings</string>
  5.     <string name="user_name">用户名</string>
  6.     <string name="password">密 码</string>    <!-- 空格需要全角形式的 -->
  7.     <string name="login_btn">登  录</string>    <!-- 空格需要全角形式的 -->

  8. </resources>
    
    AndroidManifest.xml  【主要是添加网络授权】

点击(此处)折叠或打开

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android=""
  3.     package="com.android.taxi"
  4.     android:versionCode="1"
  5.     android:versionName="1.0" >

  6.     <uses-sdk
  7.         android:minSdkVersion="8"
  8.         android:targetSdkVersion="17" />

  9.     <uses-permission android:name="android.permission.INTERNET" />
  10.     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
  11.     <!--
  12.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
  13.     </uses-permission>
  14.     <uses-permission android:name="android.permission.READ_PHONE_STATE" >
  15.     </uses-permission>
  16.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
  17.     </uses-permission>
  18.     <uses-permission android:name="android.permission.INTERNET" >
  19.     </uses-permission>
  20.     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
  21.     </uses-permission>
  22.     <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" >
  23.     </uses-permission>
  24.     -->

  25.     <application
  26.         android:allowBackup="true"
  27.         android:icon="@drawable/ic_launcher"
  28.         android:label="@string/app_name"
  29.         android:theme="@style/AppTheme" >
  30.         <activity
  31.             android:name="com.android.taxi.LoginActivity"
  32.             android:label="@string/app_name" >
  33.             <intent-filter>
  34.                 <action android:name="android.intent.action.MAIN" />

  35.                 <category android:name="android.intent.category.LAUNCHER" />
  36.             </intent-filter>
  37.         </activity>
  38.         <!--
  39.         <activity
  40.             android:name="net.youmi.android.AdActivity"
  41.             android:configChanges="keyboard|keyboardHidden|orientation" />

  42.         <meta-data
  43.             android:name="YOUMI_CHANNEL"
  44.             android:value="0" >
  45.         </meta-data>
  46.         -->
  47.     </application>

  48. </manifest>
    运行起来,启动“小猫,输入用户信息和密码..
    
    ”
    

    可能很多做android很久的,觉得这小case,我折腾了一天半,唉,主要的问题是线程问题,第二个是Looper.prepare()问题;源于两个异常:

    异常一>
 

点击(此处)折叠或打开

  1. 06-23 16:16:48.634: D/AndroidRuntime(1766): Shutting down VM
  2. 06-23 16:16:48.634: W/dalvikvm(1766): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
  3. 06-23 16:16:48.724: E/AndroidRuntime(1766): FATAL EXCEPTION: main
  4. 06-23 16:16:48.724: E/AndroidRuntime(1766): android.os.NetworkOnMainThreadException
  5. 06-23 16:16:48.724: E/AndroidRuntime(1766): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
  6. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
  7. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
  8. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.io.IoBridge.connect(IoBridge.java:112)
  9. 06-23 16:16:48.724: E/AndroidRuntime(1766): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
  10. 06-23 16:16:48.724: E/AndroidRuntime(1766): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
  11. 06-23 16:16:48.724: E/AndroidRuntime(1766): at java.net.Socket.connect(Socket.java:842)
  12. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.net.http.HttpConnection.(HttpConnection.java:76)
  13. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.net.http.HttpConnection.(HttpConnection.java:50)
  14. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
  15. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
  16. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
  17. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
  18. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
  19. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
  20. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
  21. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
  22. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
  23. 06-23 16:16:48.724: E/AndroidRuntime(1766): at com.android.taxi.HttpUtils.sendMessage(HttpUtils.java:68)
  24. 06-23 16:16:48.724: E/AndroidRuntime(1766): at com.android.taxi.LoginActivity$1.onClick(LoginActivity.java:94)
  25. 06-23 16:16:48.724: E/AndroidRuntime(1766): at android.view.View.performClick(View.java:4204)
  26. 06-23 16:16:48.724: E/AndroidRuntime(1766): at android.view.View$PerformClick.run(View.java:17355)
  27. 06-23 16:16:48.724: E/AndroidRuntime(1766): at android.os.Handler.handleCallback(Handler.java:725)
  28. 06-23 16:16:48.724: E/AndroidRuntime(1766): at android.os.Handler.dispatchMessage(Handler.java:92)
  29. 06-23 16:16:48.724: E/AndroidRuntime(1766): at android.os.Looper.loop(Looper.java:137)
  30. 06-23 16:16:48.724: E/AndroidRuntime(1766): at android.app.ActivityThread.main(ActivityThread.java:5041)
  31. 06-23 16:16:48.724: E/AndroidRuntime(1766): at java.lang.reflect.Method.invokeNative(Native Method)
  32. 06-23 16:16:48.724: E/AndroidRuntime(1766): at java.lang.reflect.Method.invoke(Method.java:511)
  33. 06-23 16:16:48.724: E/AndroidRuntime(1766): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  34. 06-23 16:16:48.724: E/AndroidRuntime(1766): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  35. 06-23 16:16:48.724: E/AndroidRuntime(1766): at dalvik.system.NativeStart.main(Native Method)
    这里的原因大概是:我们如果是想启用网络方面的服务的话,不能在主线程中进行,需要单独开辟线程,实行异步访问;但是之前的老罗的视频里面没事,估计是跟android版本有关,因此最好还是开辟线程进行,否则跑不出来会很不爽的,我就是这样被折腾到半夜;

    异常二> 

点击(此处)折叠或打开

  1. 06-23 16:16:48.634: D/AndroidRuntime(1766): Shutting down VM
  2. 06-23 16:16:48.634: W/dalvikvm(1766): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
  3. 06-23 16:16:48.724: E/AndroidRuntime(1766): FATAL EXCEPTION: main
  4. 06-23 16:16:48.724: E/AndroidRuntime(1766): android.os.NetworkOnMainThreadException
  5. 06-23 16:16:48.724: E/AndroidRuntime(1766): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
  6. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
  7. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
  8. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.io.IoBridge.connect(IoBridge.java:112)
  9. 06-23 16:16:48.724: E/AndroidRuntime(1766): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
  10. 06-23 16:16:48.724: E/AndroidRuntime(1766): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
  11. 06-23 16:16:48.724: E/AndroidRuntime(1766): at java.net.Socket.connect(Socket.java:842)
  12. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.net.http.HttpConnection.(HttpConnection.java:76)
  13. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.net.http.HttpConnection.(HttpConnection.java:50)
  14. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
  15. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
  16. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
  17. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
  18. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
  19. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
  20. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
  21. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
  22. 06-23 16:16:48.724: E/AndroidRuntime(1766): at libcore.net.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
  23. 06-23 16:16:48.724: E/AndroidRuntime(1766): at com.android.taxi.HttpUtils.sendMessage(HttpUtils.java:68)
  24. 06-23 16:16:48.724: E/AndroidRuntime(1766): at com.android.taxi.LoginActivity$1.onClick(LoginActivity.java:94)
  25. 06-23 16:16:48.724: E/AndroidRuntime(1766): at android.view.View.performClick(View.java:4204)
  26. 06-23 16:16:48.724: E/AndroidRuntime(1766): at android.view.View$PerformClick.run(View.java:17355)
  27. 06-23 16:16:48.724: E/AndroidRuntime(1766): at android.os.Handler.handleCallback(Handler.java:725)
  28. 06-23 16:16:48.724: E/AndroidRuntime(1766): at android.os.Handler.dispatchMessage(Handler.java:92)
  29. 06-23 16:16:48.724: E/AndroidRuntime(1766): at android.os.Looper.loop(Looper.java:137)
  30. 06-23 16:16:48.724: E/AndroidRuntime(1766): at android.app.ActivityThread.main(ActivityThread.java:5041)
  31. 06-23 16:16:48.724: E/AndroidRuntime(1766): at java.lang.reflect.Method.invokeNative(Native Method)
  32. 06-23 16:16:48.724: E/AndroidRuntime(1766): at java.lang.reflect.Method.invoke(Method.java:511)
  33. 06-23 16:16:48.724: E/AndroidRuntime(1766): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
  34. 06-23 16:16:48.724: E/AndroidRuntime(1766): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
  35. 06-23 16:16:48.724: E/AndroidRuntime(1766): at dalvik.system.NativeStart.main(Native Method)
  36. 06-23 16:21:49.444: I/Process(1766): Sending signal. PID: 1766 SIG: 9
  37. 06-23 16:30:10.974: E/Trace(2062): error opening trace file: No such file or directory (2)
  38. 06-23 16:30:13.324: D/dalvikvm(2062): GC_CONCURRENT freed 128K, 9% free 2665K/2916K, paused 74ms+165ms, total 289ms
  39. 06-23 16:30:13.974: D/gralloc_goldfish(2062): Emulator without GPU emulation detected.
  40. 06-23 16:30:16.664: D/InputEventConsistencyVerifier(2062): KeyEvent: ACTION_UP but key was not down.
  41. 06-23 16:30:16.664: D/InputEventConsistencyVerifier(2062): in android.widget.LinearLayout{40d1eb58 V.E..... ......I. 0,127-720,204}
  42. 06-23 16:30:16.664: D/InputEventConsistencyVerifier(2062): 0: sent at 3042487000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_N, scanCode=49, metaState=0, flags=0x8, repeatCount=0, eventTime=3042487, downTime=3042467, deviceId=0, source=0x101 }
  43. 06-23 16:30:16.714: D/InputEventConsistencyVerifier(2062): KeyEvent: ACTION_UP but key was not down.
  44. 06-23 16:30:16.714: D/InputEventConsistencyVerifier(2062): in android.widget.LinearLayout{40d1eb58 V.E..... ......I. 0,127-720,204}
  45. 06-23 16:30:16.714: D/InputEventConsistencyVerifier(2062): 0: sent at 3042645000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=3042645, downTime=3042467, deviceId=0, source=0x101 }
  46. 06-23 16:30:16.714: D/InputEventConsistencyVerifier(2062): -- recent events --
  47. 06-23 16:30:16.714: D/InputEventConsistencyVerifier(2062): 1: sent at 3042487000000, (unhandled) KeyEvent { action=ACTION_UP, keyCode=KEYCODE_N, scanCode=49, metaState=0, flags=0x80000008, repeatCount=0, eventTime=3042487, downTime=3042467, deviceId=0, source=0x101 }
  48. 06-23 16:30:19.214: W/dalvikvm(2062): threadid=11: thread exiting with uncaught exception (group=0x40a71930)
  49. 06-23 16:30:19.304: E/AndroidRuntime(2062): FATAL EXCEPTION: Thread-108
  50. 06-23 16:30:19.304: E/AndroidRuntime(2062): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
  51. 06-23 16:30:19.304: E/AndroidRuntime(2062): at android.os.Handler.(Handler.java:197)
  52. 06-23 16:30:19.304: E/AndroidRuntime(2062): at android.os.Handler.(Handler.java:111)
  53. 06-23 16:30:19.304: E/AndroidRuntime(2062): at android.widget.Toast$TN.(Toast.java:324)
  54. 06-23 16:30:19.304: E/AndroidRuntime(2062): at android.widget.Toast.(Toast.java:91)
  55. 06-23 16:30:19.304: E/AndroidRuntime(2062): at android.widget.Toast.makeText(Toast.java:238)
  56. 06-23 16:30:19.304: E/AndroidRuntime(2062): at com.android.taxi.LoginActivity$3.run(LoginActivity.java:149)
  57. 06-23 16:30:19.304: E/AndroidRuntime(2062): at java.lang.Thread.run(Thread.java:856)
  58. 06-23 16:30:24.743: I/Process(2062): Sending signal. PID: 2062 SIG: 9
    Looper用于封装了android线程中的消息循环,默认情况下一个线程是不存在消息循环(message loop)的,需要调用Looper.prepare()来给线程创建一个消息循环,调用Looper.loop()来使消息循环起作用,从消息队列里取消息,处理消息。
终于可以了。。那时已经是半夜一点半了.....Are you fresher?yes...    对了,里面我的广告代码我都注释了,没调通,周末继续.......感谢老罗,感谢愿意分享的朋友...



阅读(748) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:没有了

给主人留下些什么吧!~~