HttpClient的用法(包含httpget和httppost)

原创 2016年05月31日 09:31:15
<span style="font-size:18px;">public class MainActivity extends Activity {
	String key = "3ac9f31ff66b9746539472887b3799c3";
	// 接口地址
	String path = "http://web.juhe.cn:8080/constellation/getAll";
	// 通过get请求时的接口地址
	String get_path = "http://web.juhe.cn:8080/constellation/getAll?consName=狮子座&type=today&key="
			+ key;
	private EditText editText;

	Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {

			Bean bean = (Bean) msg.obj;

			// textView.setText(bean.date+"\n"+bean.name+"\n"+bean.finance+"\n"+bean.luckyStone+"\n"+bean.love.get(0));

			textView.setText(bean.toString());
		};

	};
	private TextView textView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		editText = (EditText) findViewById(R.id.editText);
		textView = (TextView) findViewById(R.id.textView);

	}

	/**
	 * 点击请求网络
	 */
	public void send(View v) {
		// 获取输入框的内容
		final String name = editText.getText().toString().trim();
		// 如果输入框有内容就根据输入的星座名称请求网络
		if (!TextUtils.isEmpty(name)) {
			new Thread() {

				public void run() {

//					httpGet(name);
					httpPost(name);
				};

			}.start();

		} else {

			Toast.makeText(MainActivity.this, "  请输入星座 ", 1).show();
		}

	}

	/**
	 * GET方式请求网络
	 */
	<span style="color:#ff0000;">private void httpGet(String name) {</span>
		// HttpClient得到HttpClient对象
		HttpClient httpClient = new DefaultHttpClient();
		// 以get方式请求.并设置接口地址
		HttpGet httpGet = new HttpGet(path + "consName=" + name
				+ "&type=year&key=3ac9f31ff66b9746539472887b3799c3");
		try {
			// 连接网络请求数据,请求到的数据在httpResponse对象里
			HttpResponse httpResponse = httpClient.execute(httpGet);
			// 服务器返回的内容在HttpEntity
			HttpEntity entity = httpResponse.getEntity();
			// 把entity转成String
			String string = EntityUtils.toString(entity);

			// System.out.println(string);

			Gson gson = new Gson();

			Bean bean = gson.fromJson(string, Bean.class);
			Message msg = Message.obtain();
			msg.obj = bean;
			handler.sendMessage(msg);

		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * POST方式请求
	 * 
	 * @param name
	 */
	<span style="color:#ff0000;">protected void httpPost(String name) </span>{
	
		
		HttpClient httpclient=new DefaultHttpClient();
//通过POST方式请求
		HttpPost httpPost=new HttpPost(path);
		
		//得到一个集合,这个集合里添加的是要传递给服务器的参数
		List<NameValuePair> parameters=new ArrayList<NameValuePair>();
                              //consName=狮子座
		                      //type=today
		//以键值对的形式添加参数
		parameters.add(new BasicNameValuePair("consName", name));
		parameters.add(new BasicNameValuePair("type", "year"));
		parameters.add(new BasicNameValuePair("key", key));

		try {
			//设置实体内容和编码格式
		UrlEncodedFormEntity encodedFormEntity=new UrlEncodedFormEntity(parameters, "UTF-8");
		//设置实体,用于传递给服务器参数
		httpPost.setEntity(encodedFormEntity);
			//请求网络
			HttpResponse httpResponse = httpclient.execute(httpPost);
			
			//先得到状态行,从状态行里得到状态码
			if(	httpResponse.getStatusLine().getStatusCode()==200){
				//得到实体
				HttpEntity entity = httpResponse.getEntity();
				
				//请求到的json
				String string = EntityUtils.toString(entity);
				
				Gson gson=new Gson();
				Bean bean = gson.fromJson(string, Bean.class);
				
				
				Message msg=Message.obtain();
				msg.obj=bean;
				handler.sendMessage(msg);
			}
		
			
			
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}</span>







版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

android开发httpGet httpPost httpURLConnection httpClient

一、HttpClinet方式1、HTTP GET 示例: 1 public class TestHttpGetMethod{ 2 public void get(){ 3 ...

HTTPClient模块的HttpGet和HttpPost

Android SDK集成了Apache HttpClient模块。要注意的是,这里的Apache HttpClient模块是HttpClient 4.0(org.apache.http.*),而不是...

Java的HttpClient、HttpGet和HttpPost请求

public static String getHttp(String url) { String result = ""; try { // 根据地址获取请求 HttpGet request = n...

HttpGet/HttpPost/HttpClient介绍

在Android中HttpGet/HttpPost/HttpClient的使用

Android - HTTPClient模块的HttpGet和HttpPost

说明: 目前,要为另一个项目提供接口,接口是用HTTP URL实现的,最初的想法是另一个项目用JQuery post进行请求。 但是,很可能另一个项目是部署在别的机器上,那么就存在跨域问题,而JQ...

利用HTTPClient模块中HttpGet和HttpPost 发送接口请求

HttpClient常用HttpGet和HttpPost这两个类,分别对应Get方式和Post方式。            无论是使用HttpGet,还是使用HttpPost,都必须通过如下3步来访...

Java乔晓松-httpURLConnection、URL、httpClient、httpPost、httpGet之代码详解篇

package com.example.lesson09_login; import java.io.ByteArrayOutputStream; import java.io.IOExcep...

HttpClient模块的HttpGet和HttpPost及Connection to refuse解决

Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT, DELETE,分别对应查,改,增,删四种操作。Android中最常用的是HttpGet和HttpPost ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)