您的位置:首页 > 理论基础 > 计算机网络

android http请求服务器

2018-01-13 15:32 295 查看

HTTP GET请求是把参数拼接到url中

/*

*HTTP get请求服务器

*/

 public String getData() {

  //get的方式提交就是url拼接的方式

                String url = "http://192.168.11.6:8080/?name="+123+"&passwork="+123;

                try {

                    URL httpUrl = new URL(url);

                    HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();//与服务器建立连接;

                    conn.setReadTimeout(5000);

//设置请求方式为GET

                    conn.setRequestMethod("GET");

//把获取的数据不断存放到StringBuffer中;

                    final StringBuffer sb = new StringBuffer();

//把获取的数据不断存放到StringBuffer中;

               BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

                    String line;

//只要还没有读取完,就不断读取;

                    while ((line = reader.readLine()) != null) {

//在StringBuffer中添加;

                        sb.append(line);

                    }

                    String toString = sb.toString();

                    Log.i(TAG, "----token--->:" + toString);

                   
4000
JSONObject jsonObject = new JSONObject(toString);

                    String openid = jsonObject.getString("openid").toString().trim();

                    String access_token = jsonObject.getString("access_token").toString().trim();

//                    getUserMesg(access_token, openid);

                    getNetWorkLogin(openid, "111111", adresseMAC);

                } catch (ProtocolException e1) {

                    e1.printStackTrace();

                } catch (MalformedURLException e1) {

                    e1.printStackTrace();

                } catch (IOException e1) {

                    e1.printStackTrace();

                } catch (JSONException e) {

                    e.printStackTrace();

                }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android HTTP get请求