private void getid(){ new Thread(){ @Override public void run() { try { String data="userPhone="+s_phone+"&userPassword="+s_pass; URL url=new URL(urla); HttpURLConnection openConnection= (HttpURLConnection) url.openConnection(); openConnection.setRequestMethod("POST"); openConnection.setConnectTimeout(5000); openConnection.setReadTimeout(5000); openConnection.setDoOutput(true);// 准备写出 openConnection.getOutputStream().write(data.getBytes());// 写出数据 openConnection.connect(); if (openConnection.getResponseCode()==200) { InputStream in = openConnection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(in, "utf-8")); String str; StringBuffer sb = new StringBuffer(); while ((str = br.readLine()) != null) { sb.append(str); } Message message = new Message(); message.what=0; message.obj=sb.toString(); handler.sendMessage(message); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }.start(); }
private void getid() { new Thread() { @Override public void run() { try { URL url = new URL(urla); //URL链接对象,通过URL对象打开一个connection链接对像 HttpURLConnection openConnection = (HttpURLConnection) url.openConnection(); openConnection.setRequestProperty("Accept-Charset", "GBK"); openConnection.setRequestProperty("contentType", "GBK"); openConnection.setConnectTimeout(5 * 1000); openConnection.setRequestMethod("GET"); openConnection.connect(); int flag = openConnection.getResponseCode(); if (flag == 200) { //调用urlConnection.getInputStream得到本次请求所返回的结果流 InputStream inStream = openConnection.getInputStream(); BufferedReader in = new BufferedReader(new InputStreamReader(inStream, "GBK")); String readerli; StringBuffer buffer = new StringBuffer(); while ((readerli = in.readLine()) != null) { buffer.append(readerli); } Message message = new Message(); message.what = 0; message.obj = buffer.toString(); handler.sendMessage(message); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }.start(); }