HttpClient 方法 一定要加useLibrary 'org.apache.http.legacy'

   @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        MyTask task=new MyTask();
        task.execute("http://120.27.23.105/user/getDefaultAddr?uid=71");

    }

class MyTask extends AsyncTask{


    @Override
    protected String doInBackground(String... strings) {

      HttpClient client=new DefaultHttpClient();
        HttpGet get=new HttpGet(strings[0]);

        try {
            HttpResponse execute = client.execute(get);
            if (execute.getStatusLine().getStatusCode()==200){

                HttpEntity entity = execute.getEntity();

                String s = EntityUtils.toString(entity);

                return s;

            }


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


        return null;
    }


    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        Gson gson=new Gson();
        Dateb dateb = gson.fromJson(s, Dateb.class);

        String code = dateb.getCode();
        String msg = dateb.getMsg();

        name.setText(code);
        froms.setText(msg);
    }


    }

你可能感兴趣的:(HttpClient 方法 一定要加useLibrary 'org.apache.http.legacy')