博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
指尖上的Android之实战篇(三)
阅读量:4212 次
发布时间:2019-05-26

本文共 3521 字,大约阅读时间需要 11 分钟。

先给出我们用到的工具类

1.发送请求的工具类

本实例采用HttpClient与服务器通信,用到了一个工具类对Httpclient进行封装:定义了两个方法来发送请求

getRequest:发送GET请求

postRequest :发送POST请求

HttpUtil.java(注意IP地址换成自己的IP地址,这个BASE_URL = "http://losthost:8080/auction/,要不然,你就大哭)

package com.infy.auction.client.util;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Map;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import android.util.Log;public class HttpUtil {	private static final String TAG="HttpUtil";	//创建HttpCilent对象	public static HttpClient httpClient = new DefaultHttpClient();	public static final String BASE_URL = "http://losthost:8080/auction/";		//发送url的请求,服务器响应字符串	public static String getRequest(String url) throws  Exception{		//创建一个HttpGet对象		HttpGet get = new HttpGet(url);		//发送GET请求		HttpResponse httpResponse = httpClient.execute(get);		Log.i(TAG, "getReq ==getStatusCode--->" +httpResponse.getStatusLine().getStatusCode());		//如果服务器成功地返回响应		if(httpResponse.getStatusLine().getStatusCode() == 200){			//获取响应的字符串			String result = EntityUtils.toString(httpResponse.getEntity());			Log.i(TAG, "getReq ==result--->" +result);			return result;		}		return "";	}	//发送Post请求	public static String postRequest(String url,Map
rawParams) throws Exception{ Log.i(TAG, "postRequest--->begin"); //创建HttpPost对象 HttpPost post = new HttpPost(url); //如果传递的参数个数比较多,可以对传递的参数进行封装 List
params = new ArrayList
(); for(String key:rawParams.keySet()){ //封装请求的参数 params.add(new BasicNameValuePair(key, rawParams.get(key))); } //设置请求的参数 post.setEntity(new UrlEncodedFormEntity(params,"utf-8")); //发送Post请求 HttpResponse httpResponse = httpClient.execute(post); //如果服务器成功地返回响应 Log.i(TAG, "HttpL---->" +httpResponse.getStatusLine().getStatusCode()); if(httpResponse.getStatusLine().getStatusCode() == 200){ //获取响应的字符串 String result = EntityUtils.toString(httpResponse.getEntity()); Log.i(TAG, "response-->" +result); return result; } return null; } }

2. 显示各种对话框的工具类:Dialog.xml

package com.infy.auction.client.util;import android.app.Activity;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.view.View;public class DialogUtil {	//定义一个显示消息的对话框	public static void showDialog(final Context ctx , String msg, boolean closeSelf) {		// TODO Auto-generated method stub		//创建一个AlertDialog.Builder对象		AlertDialog.Builder builder = new AlertDialog.Builder(ctx).setMessage(msg).setCancelable(false);				if(closeSelf){			builder.setPositiveButton("确定", new OnClickListener() {								@Override				public void onClick(DialogInterface dialog, int which) {					// TODO Auto-generated method stub				//结束当前Activity					((Activity)ctx).finish();				}			});		}else{			builder.setPositiveButton("确定", null);		}		builder.create().show();	}	//定义一个显示指定组件的对话框	public static void showDialog(Context ctx,View view){		AlertDialog.Builder  builder = new AlertDialog.Builder(ctx).setView(view).setCancelable(false).setPositiveButton("确定", null);		builder.create().show();	}}

 

你可能感兴趣的文章
Android编译系统环境初始化过程分析
查看>>
user2eng 笔记
查看>>
DRM in Android
查看>>
ARC MRC 变换
查看>>
Swift cell的自适应高度
查看>>
【linux】.fuse_hiddenXXXX 文件是如何生成的?
查看>>
【LKM】整合多个LKM为1个
查看>>
【Windows C++】调用powershell上传指定目录下所有文件
查看>>
Java图形界面中单选按钮JRadioButton和按钮Button事件处理
查看>>
小练习 - 排序:冒泡、选择、快排
查看>>
SparkStreaming 如何保证消费Kafka的数据不丢失不重复
查看>>
Spark Shuffle及其调优
查看>>
数据仓库分层
查看>>
常见数据结构-TrieTree/线段树/TreeSet
查看>>
Hive数据倾斜
查看>>
TopK问题
查看>>
Hive调优
查看>>
HQL排查数据倾斜
查看>>
DAG以及任务调度
查看>>
LeetCode——DFS
查看>>