当安装这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); } }; }
那不是报数组越界错误吗。。。length=1,index=1 这里这是因为获取到了/favion.ico才报数组越界这个错误的吧?这个/favion.ico是后面才出来的,我要刚开始获取到的那个请求参数就就好了,然后断开连接。<divclass="ref">
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。