eclipse可能需要org.apache.http.legacy.jar,该jar包在sdk/platforms/android-xx(您所安装的最高的sdk版本)/optional目录下可以找到
1 // 网络访问请求 2 new Thread(new Runnable() { 3 4 @Override 5 public void run() { 6 7 String url = ""; 8 // 创建httpCient 9 HttpClient client = new DefaultHttpClient(); 10 11 // 创建post请求 12 HttpPost post = new HttpPost(url); 13 List<NameValuePair> pairs = new ArrayList<NameValuePair>(); 14 // 配置请求头 15 post.addHeader("", ""); 16 17 // 配置请求参数 18 pairs.add(new BasicNameValuePair("", "")); 19 pairs.add(new BasicNameValuePair("", "")); 20 21 try { 22 post.setEntity(new UrlEncodedFormEntity(pairs)); 23 HttpResponse response = client.execute(post); 24 25 int statusCode = response.getStatusLine().getStatusCode(); 26 if (statusCode == 200) { 27 // 拿到网络的返回结果 28 String result = EntityUtils.toString(response.getEntity()); 29 30 // 需要在主线程中进行UI操作 31 Toast.makeText(context, result, Toast.LENGTH_SHORT).show(); 32 } 33 } catch (Exception e) { 34 // TODO: handle exception 35 } 36 } 37 }).start();
时间: 2024-10-25 03:50:55