开发者社区> 问答> 正文

访问android的socket服务端出问题?报错

当安装这android程序到手机并且运行后,在android的浏览器中访问http://127.0.0.1:9999/printLabel?userName=中国人&orderNo=1354&areaNo=2这个网址后发现会自动有很多请求过去,程序就一直获取参数,而且运行到后面会获取到一个/favion.ico,然后这个android程序就角标越界异常停止运行了。请问如何解决这些问题?我只需要获取到一次请求参数就可以了,获取之后就断开浏览器的当次连接,等待下一次连接。报错如下图:

android程序代码如下:

public class MainActivity extends Activity {
		private static EditText etUserName;
		private static EditText etAreaNo;
		private static EditText etOrderNo;
		private Button button1;
		private Button button2;
		private ServerSocket ss;
		private Socket s;
		private static Handler handler; 
		private static String userName;
		private static String orderNo;
		private static String areaNo;
		
		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.main);
			etport = (EditText) findViewById(R.id.etport);
			etbluetooth = (EditText) findViewById(R.id.etbluetooth);
			button1 = (Button) findViewById(R.id.button1);
			button2 = (Button) findViewById(R.id.button2);
			etUserName = (EditText)findViewById(R.id.etUserName);
			etAreaNo = (EditText) findViewById(R.id.etAreaNo);
			etOrderNo = (EditText) findViewById(R.id.etOrderNo);
			 //创建属于主线程的handler  
	                handler=new Handler();  
			new Thread(new Runnable() {
				public void run() {
			try {
				ss = new ServerSocket(9045);
				while(true){
				s = ss.accept();
					invoke(s);
				}
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
				}}).start();
		}
			private static void invoke(final Socket s) throws IOException {
			//创建新线程
			new Thread(new Runnable() {
				public void run() {
					BufferedReader in = null;
					try {
						in = new BufferedReader(new InputStreamReader(
								s.getInputStream()));
						while (true) {
			                        String data = in.readLine();
							if (data == null) {
								return;
							}else{
								try {
						    data = URLDecoder.decode(data, "UTF-8");
						} catch (UnsupportedEncodingException e) {
							e.printStackTrace();
						}
						System.out.println("客户端传过来的数据为:" + data);
						if (data.startsWith("GET")) {
							String a = data.split("HTTP")[0];
							String sid = a.split("\\?")[1];
							String[] sts = sid.split("&");
							userName = sts[0].split("=")[1];
							orderNo = sts[1].split("=")[1];
							areaNo = sts[2].split("=")[1];
						}
			System.out.println("userName=" + userName + ","+ "orderNo=" + orderNo + ","+ "areaNo=" + areaNo);
						handler.post(runnableUi);
					}break;
				}
					} catch (IOException ex) {
						ex.printStackTrace();
					} finally {
						try {
							in.close();
						} catch (Exception e) {
						}
						try {
							s.close();
						} catch (Exception e) {
						}
					}
				}
			}).start();
		}
		// 构建Runnable对象,在runnable中更新界面  
	    static Runnable runnableUi=new  Runnable(){  
	        @Override  
	        public void run() {  
	            //更新界面  
	        	etUserName.setText(userName);
				etAreaNo.setText(areaNo);
				etOrderNo.setText(orderNo); 
	        }  
	    };  
	}


展开
收起
爱吃鱼的程序员 2020-06-22 15:25:54 519 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    那不是报数组越界错误吗。。。length=1,index=1 这里这是因为获取到了/favion.ico才报数组越界这个错误的吧?这个/favion.ico是后面才出来的,我要刚开始获取到的那个请求参数就就好了,然后断开连接。<divclass="ref">

    引用来自“backtract”的答案

    <divclass=ref_body>那不是报数组越界错误吗。。。length=1,index=1 这里有没人再帮忙看看。。。有没人啊。。。你可以在  System.out.println("客户端传过来的数据为:"<spanstyle="font-family:Consolas,'BitstreamVeraSansMono','CourierNew',Courier,monospace;line-height:14.659090995788574px;background-color:#FFFFFF;"> +data);这里判断一下啊,看data是不是为null如果不是null那就是获取到数据了,然后就直接break最后那个更新UI代码注释悼,如不再报错。把UI更新放到Handler中。<divclass="ref">

    引用来自“张新武”的答案

    <divclass=ref_body>你可以在  System.out.println("客户端传过来的数据为:"<spanstyle="font-family:Consolas,'BitstreamVeraSansMono','CourierNew',Courier,monospace;line-height:14.659090995788574px;background-color:#FFFFFF;"> +data);这里判断一下啊,看data是不是为null如果不是null那就是获取到数据了,然后就直接break<divclass="ref">

    引用来自“倪倪”的答案

    <divclass=ref_body>最后那个更新UI代码注释悼,如不再报错。把UI更新放到Handler中。还有人可以帮忙看看么。。。

    2020-06-22 15:26:13
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载