首先必须声明权限:
<uses-permission android:name="android.permission.INTERNET"/>
API接口:https://www.juhe.cn/docs/api/id/46
1 HttpURLConnection
Get
private void httpURLConnectionGet() { // 开启线程来发起网络请求 new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection = null; try { URL url = new URL(URL_GET+"key="+API_KEY+"&menu="+MENU); //获取HttpURLConnection实例 connection = (HttpURLConnection) url.openConnection(); //GET表示希望从服务器获取数据 connection.setRequestMethod("GET"); //设置连接超时、读取超时的毫秒数 connection.setConnectTimeout(8000); connection.setReadTimeout(8000); getURLConnectionResponse(connection); } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { //关闭HTTP连接 connection.disconnect(); } } } }).start(); }
Post
private void httpURLConnectionPost() { // 开启线程来发起网络请求 new Thread(new Runnable() { @Override public void run() { HttpURLConnection connection = null; try { URL url = new URL(URL_POST); //获取HttpURLConnection实例 connection = (HttpURLConnection) url.openConnection(); //GET表示希望从服务器获取数据 connection.setRequestMethod("POST"); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); //不解码会乱码 out.writeBytes("key="+API_KEY+"&menu="+URLEncoder.encode(MENU,"UTF-8")); //设置连接超时、读取超时的毫秒数 connection.setConnectTimeout(8000); connection.setReadTimeout(8000); getURLConnectionResponse(connection); } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { //关闭HTTP连接 connection.disconnect(); } } } }).start(); }
getURLConnectionResponse
private void getURLConnectionResponse(HttpURLConnection connection) throws Exception { //获取服务器返回的输入流 InputStream in = connection.getInputStream(); // 利用BufferedReader对服务器返回的流进行读取 BufferedReader reader = new BufferedReader( new InputStreamReader(in)); StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } sendMessage(response.toString()); }
2 HttpClient
Get
private void httpClientGet() { new Thread(new Runnable() { @Override public void run() { try { //创建一个HttpClient实例 HttpClient httpClient = new DefaultHttpClient(); //创建一个HttpGet对象以发起GET请求 //指定访问的服务器地址是电脑本机 HttpGet httpGet = new HttpGet(URL_GET+"key="+API_KEY+"&menu="+MENU); HttpResponse httpResponse = httpClient.execute(httpGet); getHttpClient(httpResponse); } catch (Exception e) { e.printStackTrace(); } } }).start(); }
Post
private void httpClientPost() { new Thread(new Runnable() { @Override public void run() { try { //创建一个HttpClient实例 HttpClient httpClient = new DefaultHttpClient(); //创建一个HttpGet对象以发起GET请求 //指定访问的服务器地址是电脑本机 HttpPost httpPost = new HttpPost(URL_POST); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("key", API_KEY)); params.add(new BasicNameValuePair("menu", MENU)); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8"); httpPost.setEntity(entity); HttpResponse httpResponse = httpClient.execute(httpPost); getHttpClient(httpResponse); } catch (Exception e) { e.printStackTrace(); } } }).start(); }
getHttpClient
private void getHttpClient(HttpResponse httpResponse) throws Exception{ if (httpResponse.getStatusLine().getStatusCode() == 200) { // 请求和响应都成功了 HttpEntity entity1 = httpResponse.getEntity(); String response = EntityUtils.toString(entity1, "utf-8"); //parseJSONWithJSONObject(response); sendMessage(response); } }
sendMessage
private void sendMessage(String response) { Message message = new Message(); message.what = SHOW_RESPONSE; // 将服务器返回的结果存放到Message中 message.obj = response; handler.sendMessage(message); }
handler
//子线程无法操作UI,要借用Handler将服务器返回的内容显示出来 private Handler handler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case SHOW_RESPONSE: String response = (String) msg.obj; // 在这里进行UI操作,将结果显示到界面上 responseText.setText(response); } } };