클라이언트 액세스 서버 대량 데이터 표시

10129 단어 서버
서버에 대한 정보를 대량으로 가져옵니다. 로컬에서 서버에 메시지를 보내지 않고 직접 가져옵니다.
클래스를 정의하여 서버에 접근하고 이 페이지에 간단한 ListView에 표시합니다
서버 클래스 액세스
초기화 정의
// ListView	
private ListView lv_leave;
// Adapter	
LeaveInfoAdapter lva;
ArrayList<LeaveInfo> list = new ArrayList<LeaveInfo>();

 
onCreate 메서드에서 액세스 시작
                setContentView(R.layout.leave_show);
		lv_leave = (ListView) findViewById(R.id.leave_show_list);

		try {
			 
                       // list 
			list = JSONUtil.postRequest("http://172.16.10.131:8080/workflow/workapprove!phoneFindStatus02.action");

              // list Adapter
			lva = new LeaveInfoAdapter(list, this);

// Adapter ListView
        	         lv_leave.setAdapter(lva);
			 
		} catch (Exception e) {
			System.out.println(" ");
			e.printStackTrace();
		}

 
JsonUtil 해석은 가져온 값을list에 저장하고 액세스 서버 클래스에서 정의한list에 전송합니다
 
public class JSONUtil {
	private static final String TAG = "JSONUtil";

	/**
	 *  json 
	 * 
	 * @param url
	 * @return JSONArray
	 * @throws JSONException
	 * @throws ConnectionException
	 */
	public static JSONObject getJSON(String url) throws JSONException,
			Exception {

		return new JSONObject(getRequest(url));
	}

	/**
	 *  api get , 。
	 * 
	 * @param url
	 * @return String
	 */
	protected static String getRequest(String url) throws Exception {
		return getRequest(url, new DefaultHttpClient(new BasicHttpParams()));
	}

	protected static ArrayList<LeaveInfo> postRequest(String url)
			throws Exception {
		return postRequest(url, new DefaultHttpClient());
	}

	/**
	 *  api get , 。
	 * 
	 * @param url
	 * @param client
	 * @return String
	 */
	protected static String getRequest(String url, DefaultHttpClient client)
			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);
			HttpResponse httpResponse = client.execute(getMethod);
			 statusCode = httpResponse.getStatusLine().getStatusCode();
			Log.d(TAG, "statuscode = " + statusCode);
		 
			result = retrieveInputStream(httpResponse.getEntity());
			 
		} catch (Exception e) {
			Log.e(TAG, e.getMessage());
			throw new Exception(e);
		} finally {
			getMethod.abort();
		}
		return result;
	}

	protected static ArrayList<LeaveInfo> postRequest(String url,
			DefaultHttpClient client) throws Exception {
		String result = null;
		int statusCode = 0;
		 ArrayList<LeaveInfo> list = new ArrayList<LeaveInfo>();
		HttpPost getMethod = new HttpPost(url);
		Log.d(TAG, "do the postRequest,url=" + url + "");
		try {
			// getMethod.setHeader("User-Agent", USER_AGENT);
			HttpResponse httpResponse = client.execute(getMethod);
			// statusCode == 200  
			statusCode = httpResponse.getStatusLine().getStatusCode();
			Log.d(TAG, "statuscode = " + statusCode);
			//  httpResponse 
			if (statusCode == 200) {

				HttpEntity entity = httpResponse.getEntity();
				result = retrieveInputStream(entity);
				
				 
				JSONArray jsonArray = new JSONArray(result );
				System.out.println(jsonArray);
				for (int i = 0, size = jsonArray.length(); i < size; i++) {
					LeaveInfo bean = new LeaveInfo();
					JSONObject jsonObject = jsonArray.getJSONObject(i);

					bean.setId(jsonObject.getInt("id"));
					bean.setApplyname(jsonObject.getString("applyname"));
					bean.setApplytime(jsonObject.getString("applytime"));
					bean.setReason(jsonObject.getString("reason"));
					bean.setStatus(jsonObject.getString("status"));
					list.add(bean);
				}
				 
				
			}

		} catch (Exception e) {
			// Log.e(TAG, e.getMessage());
			throw new Exception(e);
		} finally {
			// getMethod.abort();
		}
		return list;
	}

	/**
	 *  httpResponse , String
	 * 
	 * @param httpEntity
	 * @return String
	 */
	protected static String retrieveInputStream(HttpEntity httpEntity) {

		int length = (int) httpEntity.getContentLength();
		// the number of bytes of the content, or a negative number if unknown.
		// If the content length is known but exceeds Long.MAX_VALUE, a negative
		// number is returned.
		// length==-1, ,println needs a message
		if (length < 0)
			length = 10000;
		StringBuffer stringBuffer = new StringBuffer(length);
		try {
			InputStreamReader inputStreamReader = new InputStreamReader(
					httpEntity.getContent(), "UTF-8");
			char buffer[] = new char[length];
			int count;
			while ((count = inputStreamReader.read(buffer, 0, length - 1)) > 0) {
				stringBuffer.append(buffer, 0, count);
			}
		} catch (UnsupportedEncodingException e) {
			Log.e(TAG, e.getMessage());
		} catch (IllegalStateException e) {
			Log.e(TAG, e.getMessage());
		} catch (IOException e) {
			Log.e(TAG, e.getMessage());
		}
		System.out.println("resulte:" + stringBuffer.toString());
		return stringBuffer.toString();
	}

}

액세스 서버 클래스를 채우는 Adapter
 
public class LeaveInfoAdapter extends BaseAdapter {
	List<LeaveInfo>  list;
	Context mcontext;

	public LeaveInfoAdapter(List<LeaveInfo> list, Context context) {

		this. list = list;
		this.mcontext = context;
	}

	public void setResult(List<LeaveInfo> list, Context context) {
		this. list = list;
		this.mcontext = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return  list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(mcontext);
			convertView = inflater.inflate(R.layout.leave, null);
		}
		TextView tv_name = (TextView) convertView.findViewById(R.id.leave_name);
		tv_name.setText( " :"+list.get(position).getApplyname());
		TextView tv_time = (TextView) convertView.findViewById(R.id.leave_time);
		tv_time.setText(" "+ list.get(position).getApplytime());
		TextView tv_reason = (TextView) convertView
				.findViewById(R.id.leave_reason);
		tv_reason.setText( " "+list.get(position).getReason());
		TextView tv_status = (TextView) convertView
				.findViewById(R.id.leave_status);
		tv_status.setText( " :"+list.get(position).getStatus());

		return convertView;
	}

}

 
LeaveInfo 솔리드 객체
 
public class LeaveInfo {

private int id ;
private String applyname  ;
private String applytime  ;
private String reason ;
private String status  ;
 Getters Setters 
 

}

그리고 leave를 정의하는 거예요.show.xml
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    
<ListView 
    android:id="@+id/leave_show_list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"></ListView>
</LinearLayout>

 
Adapter를 채우는 XMl leave를 정의합니다.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TextView
        android:id="@+id/leave_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

    <TextView
        android:id="@+id/leave_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <TextView
        android:id="@+id/leave_reason"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
          />

    <TextView
        android:id="@+id/leave_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
 
</LinearLayout>

 
위 코드는android 클라이언트가 서버에 직접 접근하여 데이터를 얻고 로컬에 표시한 ListView의 실례입니다

좋은 웹페이지 즐겨찾기