jar:
ezmorph-1.0.6.jar
commons-lang 2.4
commons-beanutils 1.7.0
commons-collections 3.2
commons-logging 1.1.1
json-lib-2.1.jar
jsonplugin-0.34.jar
package com.aven.net;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class PostActivity extends Activity {
private Button backBtn;
private TextView httpTv;
@SuppressWarnings("unused")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.http);
backBtn = (Button) findViewById(R.id.backBtn);
httpTv = (TextView) findViewById(R.id.httpTv);
String httpUrl = "http://192.168.0.150:8080/httpget.jsp?";
String resultData = "";
HttpPost httpRequest = new HttpPost(httpUrl);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("par", "大家好"));
try {
HttpEntity httpEntity = new UrlEncodedFormEntity(params, "UTF-8");
httpRequest.setEntity(httpEntity);
HttpParams httpParams=new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 2000);
HttpClient httpClient = new DefaultHttpClient(httpParams);
HttpResponse httpResponse = httpClient.execute(httpRequest);
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String strResult = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");
httpTv.setText(Html.fromHtml(strResult));
} else {
httpTv.setText("读取的内容为NULL");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
// URL url = null;
// try {
// url = new URL(httpUrl);
// } catch (Exception e) {
// System.out.println(e.getMessage());
// }
// if (url != null) {
// try {
// HttpURLConnection urlConnection = (HttpURLConnection)
// url.openConnection();
// urlConnection.setConnectTimeout(2000);
// urlConnection.setDoInput(true);
// urlConnection.setDoOutput(true);
// urlConnection.setRequestMethod("POST");
// urlConnection.setUseCaches(false);
// urlConnection.setInstanceFollowRedirects(true);
// urlConnection.setRequestProperty("Content-Type",
// "application/x-www-form-urlencoded");
// urlConnection.connect();
// DataOutputStream outputStream=new
// DataOutputStream(urlConnection.getOutputStream());
// String content="par="+URLEncoder.encode("你好","UTF-8");
// outputStream.writeBytes(content);
// outputStream.flush();
// outputStream.close();
// InputStream inputStream = urlConnection.getInputStream();
// InputStreamReader in = new InputStreamReader(inputStream,"UTF-8");
// BufferedReader br = new BufferedReader(in);
// String inputLine = null;
// while ((inputLine = br.readLine()) != null) {
// resultData += inputLine + "\n";
// }
// in.close();
// inputStream.close();
// urlConnection.disconnect();
// if (resultData != null) {
// httpTv.setText(Html.fromHtml(resultData));
// } else {
// httpTv.setText("读取的内容为NULL");
// }
// } catch (Exception e) {
// System.out.println(e.getMessage());
// }
// } else {
// System.out.println("Url NULL");
// }
backBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(PostActivity.this, NetConnectionDemoActivity.class);
startActivity(intent);
PostActivity.this.finish();
}
});
}
}
分享到:
相关推荐
本文并不针对HTTP协议的具体内容,仅探讨android开发中使用HTTP协议访问网络的两种方式——HttpURLConnection和HttpClient 因为需要访问网络,需在AndroidManifest.xml中添加如下权限 <uses android:name=...
下面给大家展示了AndroidHttpClient结构: public final class AndroidHttpClient extends Object implements HttpClient 前言: 这类其实是Google对阿帕奇的HttpClient的一个封装,一些默认属性有android做了一些...
Android HttpUrlConnection使用方法 post and get json.
里面详细的写了post和get和HttpPost和HttpGet请求
介绍了Android 中HttpURLConnection与HttpClient使用的简单实例,有需要的朋友可以参考一下
主要介绍了Android通过HttpURLConnection和HttpClient接口实现网络编程的相关资料,需要的朋友可以参考下
该压缩包中包含3个小程序,第一个是使用HttpURLConnection获取网络资源,包括直接获取、使用get方式获取、使用post方式获取、以及获得网络图片;第二个小程序是使用HttpClient获取网络资源,包括get方式和post方式;...
Android客户端向服务器端请求数据的两种主要方式:HTTPURLConnection、HTTPClient;内含客户端与服务器端Demo(带有详细注释),原生方式解析json数据,希望大家多多批评指教!!
早些时候,Android 上发送 HTTP 请求一般有 2 种方式:HttpURLConnection 和 HttpClient。不过由于 HttpClient 存在 API 数量过多、扩展困难等缺点,Android 团队越来越不建议我们使用这种方式。在 Android 6.0 系统...
Android 6.0之后取消了HttpClient,不支持跟新 ,今天小编使用的是HttpURLConnection : 直接上代码: URL url = null; BufferedInputStream bin = null; HttpURLConnection httpURLConnection = null; Context...
#TOPHE-类型化对象处理HTTP引擎一个Android库,使带有参数的HTTP调用更加容易。 有一个模块使用 signpost支持OAuth1签名,一个模块将用作HTTP引擎(默认情况下,它使用Java的HttpUrlConnection )。 ##特征强类型...
本例演示最简单的android客户端和服务器端通信,客户端使用android4.0以上版本,服务器端为dynamic web project工程,使用时将client...因为android6.0以上不再支持httpclient通信,故本例使用HttpURLConnection类演示
本项目包含4种情况对应httpclient/httpurlconnection以及是否需要验证服务端证书实现android通过https通信
安卓开发 网络请求实现类,包括HttpClient get/post 请求,和 HttpUrlConnection的操作!
HttpURLConnection与HttpClient volley-retrofit-okhttp之我们该如何选择网路框架 Volley源码分析 Retrofit详解(上) Retrofit详解(下) Dagger2 1.Dagger2简介(一).md 2.Dagger2入门demo(二).
Android网络框架之OkHttp是一个处理网络请求的开源项目,是Android端最火热的轻量级框架,由移动支付Square公司贡献(该公司还贡献了Picasso)用于替代HttpUrlConnection和Apache HttpClient(android API23 6.0 里...
在android项目中,经常需要用到网络,而在联网之前前,我们都要做一次网络的判断,判断当前的网络状态是否可用,然后开始请求网络。 android中请求网络方式有:HttpURLConnection和HttpClient