代码如下 | 复制代码 |
: import java.io.File; import android.content.Context; /** /** /** public AsyncImageView(Context context) { public AsyncImageView(Context context, AttributeSet attrs) { public AsyncImageView(Context context, AttributeSet attrs, int defStyle) { /** /** @Override @Override /** /** /** /** @Override @Override @Override /** File file = new File(fileName); /** /** |
代码如下 | 复制代码 |
<?php ///如果有上传文件则接收 if($_FILES){ $target_path = $target_path . basename( $_FILES['file1']['name']); try{ if(move_uploaded_file($_FILES['file1']['tmp_name'], $target_path)) { echo "The file ". basename( $_FILES['file1']['name']). " has been uploaded"; } } catch( Exception $e ) { echo $e->getMessage(); } } ?> Android 代码: package com.nbcio.baishicha.test; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.Toast;
/* * 变量声明 newName:上传后在服务器上的文件名称 * * uploadFile:要上传的文件路径 actionUrl:服务器对应的程序路径 */ private String newName = ""; private String uploadFile = ""; private String actionUrl = "http://www.111cn.net/index.php";//这里定义你的上传路径
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bundle bundle = intent.getExtras(); newName = bundle.getString("fileName"); uploadFile = newName; String ok = post(actionUrl, newName); Toast.makeText(this, "OK!", Toast.LENGTH_LONG).show(); finish();
// // TODO Auto-generated catch block e.printStackTrace(); }
/** * * @param actionUrl * @param params * @param files * @return * @throws IOException */ public static String post(String actionUrl, String FileName) throws IOException {
String PREFIX = "--", LINEND = "rn"; String MULTIPART_FROM_DATA = "multipart/form-data"; String CHARSET = "UTF-8";
HttpURLConnection conn = (HttpURLConnection) uri.openConnection(); conn.setReadTimeout(5 * 1000); // 缓存的最长时间 conn.setDoInput(true);// 允许输入 conn.setDoOutput(true);// 允许输出 conn.setUseCaches(false); // 不允许使用缓存 conn.setRequestMethod("POST"); conn.setRequestProperty("connection", "keep-alive"); conn.setRequestProperty("Charsert", "UTF-8"); conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA + ";boundary=" + BOUNDARY);
conn.getOutputStream());
if (FileName != "") {
sb1.append(PREFIX); sb1.append(BOUNDARY); sb1.append(LINEND); sb1.append("Content-Disposition: form-data; name="file1"; filename="" + FileName + """ + LINEND); sb1.append("Content-Type: application/octet-stream; charset=" + CHARSET + LINEND); sb1.append(LINEND); outStream.write(sb1.toString().getBytes());
byte[] buffer = new byte[1024]; int len = 0; while ((len = is.read(buffer)) != -1) { outStream.write(buffer, 0, len); } outStream.write(LINEND.getBytes()); byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes(); outStream.write(end_data); outStream.flush(); int res = conn.getResponseCode(); InputStream in = null; if (res == 200) { in = conn.getInputStream(); int ch; StringBuilder sb2 = new StringBuilder(); while ((ch = in.read()) != -1) { sb2.append((char) ch); } } return in == null ? null : in.toString(); } } |
下面是具体的代码
/**
* 向网站发送get请求,url需按照api要求写,返回取得的信息。
* //这个专门给大众点评传入cookie参数用,目的是为了获得用户选择的城市信息
* @param url
* @param client
* @return String
* @author lvqiyong
*/
代码如下 | 复制代码 |
public static String getRequest1(String url, DefaultHttpClient client, String charset) throws Exception { String result = null; int statusCode = 0; HttpGet getMethod = new HttpGet(url); Log.d(TAG, "do the getRequest,url=" + url + ""); try { getMethod.setHeader("User-Agent", USER_AGENT); getMethod.setHeader("Cookie", "cy=" + value);//这个专门给大众点评传入cookie参数用,目的是为了获得用户选择的城市信息 // 添加用户密码验证信息 // client.getCredentialsProvider().setCredentials( // new AuthScope(null, -1), // new UsernamePasswordCredentials(mUsername, mPassword));
HttpResponse httpResponse = client.execute(getMethod); // statusCode == 200 正常 statusCode = httpResponse.getStatusLine().getStatusCode(); Log.d(TAG, "statuscode = " + statusCode); // 处理返回的httpResponse信息 if (statusCode == 200) { result = retrieveInputStream(httpResponse.getEntity(), charset); Cookie cookie; String cookname,cookvalue; List<Cookie> cookies = client.getCookieStore().getCookies(); if (cookies.isEmpty()) { Log.i(TAG, "-------Cookie NONE---------"); } else { for (int i = 0; i < cookies.size(); i++) { // 保存cookie cookie = cookies.get(i); cookname = cookie.getName().trim(); cookvalue = cookie.getValue().trim(); if(cookname.equals("cy")){ name = cookname; value = cookvalue; } } } } else result = "networkerror"; } catch (ConnectTimeoutException e) {// 超时或网络连接出错 result = "timeouterror"; // e.printStackTrace(); } catch (ClientProtocolException e) { result = "networkerror"; // e.printStackTrace(); } catch (Exception e) { result = "readerror"; Log.e(TAG, e.getMessage()); throw new Exception(e); } finally { getMethod.abort(); } return result; } |
因为百事查需要,需要制作这样的更多分页按钮,因为感觉新浪微博的更多分页按钮比较好,就尝试做了一下。
1、首先需要考虑布局,就是footer的布局,如下:
代码如下 | 复制代码 |
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="wrap_content" android:focusable="true" xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/loadmore" android:layout_centerVertical="true" android:layout_centerHorizontal="true"> <!-- <Button android:id="@+id/loadMoreButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="更多" /> --> <LinearLayout android:gravity="center" android:layout_height="wrap_content" android:orientation="vertical" android:layout_width="fill_parent"> <ImageView android:background="@drawable/divider" android:layout_height="2.0dip" android:layout_width="fill_parent" /> </LinearLayout> <LinearLayout android:gravity="center" android:layout_gravity="center" android:orientation="horizontal" android:layout_centerVertical="true" android:layout_width="fill_parent" android:layout_height="60dip"> <TextView android:textSize="20.0sp" android:textColor="#ff545454" android:gravity="center" android:id="@id/tv_msg" android:text="更多" android:layout_width="fill_parent" android:layout_height="wrap_content"/> <LinearLayout android:gravity="center" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="horizontal" android:id="@+id/llloading" android:layout_width="fill_parent"> <ProgressBar android:layout_gravity="center_vertical" android:id="@id/footprogress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:indeterminateBehavior="repeat" style="?android:progressBarStyleSmallInverse" /> <TextView android:textColor="#ff000000" android:gravity="left|center" android:padding="2.0px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="读取中" /> </LinearLayout> </LinearLayout> </LinearLayout> <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="false" android:drawable="@drawable/listview_gradient" > </item> <item android:state_pressed="true" android:drawable="@drawable/list_selector_background_pressed" > </item> </selector>其中list_selector_background_pressed系统带的一个按钮在listview源代码中可以找到或sdk中也有 2、同时加载footer布局 list_footer = (LinearLayout)LayoutInflater.from(FansActivity.this).inflate(R.layout.sinalist_footer, null); tv_msg = (TextView)list_footer.findViewById(R.id.tv_msg); loading = (LinearLayout)list_footer.findViewById(R.id.llloading); //btloadmore = (Button)list_footer.findViewById(R.id.loadMoreButton); listView = getListView(); top_panel = (View)findViewById(R.id.fans_top); top_btn_left = (Button)top_panel.findViewById(R.id.top_btn_left); top_btn_right = (Button)top_panel.findViewById(R.id.top_btn_right); top_title = (TextView)top_panel.findViewById(R.id.top_title); listView.addFooterView(list_footer, null, false);//利用FooterVIew分页动态加载,参数false是不让选择 list_footer.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { try { // Toast.makeText(FansActivity.this, "我将消失了", // Toast.LENGTH_SHORT).show(); //list_footer.setBackgroundColor(Color.YELLOW); //list_footer.invalidate(); new FansThread().start(); //tv_msg.setEnabled(false); tv_msg.setVisibility(View.GONE);// 隐藏更多提示的TextView loading.setVisibility(View.VISIBLE);// 显示最下方的进度条 } catch (Exception e) { e.printStackTrace(); } } }); |
效果如下