Loading

HttpURLConnection和OkHttp向服务器发送请求,以及JsonObject和GSON解析Json

OKHttp:

 

private void sendRequestWithOkHttp(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                Log.d("..................","................");
                try{
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url("http://172.28.197.68/get_data.json")
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseData = response.body().string();
                    parseJSONWithGSON(responseData);//对数据进行处理
                    Log.d("..................","................");
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    Looper.prepare();
                    Toast.makeText(MainActivity.this,"请求成功",Toast.LENGTH_SHORT).show();//子线程的提醒操作需要在loop里面进行
                    Looper.loop();
                }
            }
        }).start();

  

HttpURLConnection

private void sendRequestWithHttpURLConnection(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                BufferedReader reader = null;
                try{
                    URL url = new URL("https://blog.csdn.net/qq_38740763");
                    connection = (HttpURLConnection)  url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(8000);
                    InputStream in = connection.getInputStream();
                    reader = new BufferedReader(new InputStreamReader(in));
                    StringBuffer response = new StringBuffer();
                    String line;
                    while((line=reader.readLine())!=null){
                        response.append(line);
                    }
                    showResponse(response.toString());//这一行是自定义的对请求下来的东西的处理
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    if(reader!=null){
                        try{
                            reader.close();
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                    if(connection!=null){
                        connection.disconnect();
                    }
                }
            }
        }).start();
    }

  

JsonObject解析Json文件

private void parseJSONWithJSONObject(String jsonData){
        try{
            JSONArray jsonArray = new JSONArray(jsonData);
            for (int i = 0;i < jsonArray.length();i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                String id = jsonObject.getString("id");
                String name = jsonObject.getString("name");
                String version = jsonObject.getString("version");
                Log.d("id是",id);
                Log.d("name是",name);
                Log.d("version是",version);
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

  

GSON解析Json文件

private void parseJSONWithGSON(String jsonData){
        Gson gson = new Gson();
        List<Person> personList = gson.fromJson(jsonData,new TypeToken<List<Person>>(){}.getType());
        for(Person person:personList){
            Log.d("名字 ",person.getName());
            Log.d("性别 ",person.getSex());
            Log.d("年龄 ",String.valueOf(person.getAge()));
        }
    }

  

 

posted @ 2018-06-09 11:48  1900Yin  阅读(245)  评论(0)    收藏  举报