package com.hl;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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 android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class SimplePOST extends Activity {
private TextView show;
private EditText txt;
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
show = (TextView)findViewById(R.id.show);
txt = (EditText)findViewById(R.id.txt);
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dopost(txt.getText().toString());
}
});
}
private void dopost(String val){
//封装数据
Map<String, String> parmas = new HashMap<String, String>();
parmas.put("name", val);
DefaultHttpClient client = new DefaultHttpClient();//http客户端
HttpPost httpPost = new HttpPost("http://mhycoe.com/test/post.php");
ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
if(parmas != null){
Set<String> keys = parmas.keySet();
for(Iterator<String> i = keys.iterator(); i.hasNext();) {
String key = (String)i.next();
pairs.add(new BasicNameValuePair(key, parmas.get(key)));
}
}
try {
UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(pairs, "utf-8");
/*
* 将POST数据放入HTTP请求
*/
httpPost.setEntity(p_entity);
/*
* 发出实际的HTTP POST请求
*/
HttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
String returnConnection = convertStreamToString(content);
show.setText(returnConnection);
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
分享到:
相关推荐
android http-post方法在开发中如何实现,具体代码如下,感兴趣的朋友可以参考下哈,希望对大家有所帮助
Android Studio实现POST请求,每个步骤都有注释,比较适合萌新了解如果通过POST方式发起网络请求。
Animating Google emojis with Android animated vector drawables. All credits to noto-emoji for the original emoji. Can't see your favourite emoji? Find the svg there and play with it. Then submit a PR...
android-async-http开源项目的GET方式或POST方式实现登陆案例简单Demo
Cordova-Plugin-Http,基于iOS,Android平台的插件,可实现get、post请求。
Retrofit Okhttp辅助类的简单封装,vesion 1.0.X 实现了Get,Post-Form、Post-Json 三种形式的网络请求,后续版本会实现文件上传下载and各类raw的请求方式。
HttpClient工具类,简单轻松的实现get,post,put和delete请求
利用httppost实现和服务器的通讯,需要修改下客户端的ip地址,如果没有联网,设置为10.0.2.2
Android Android Study Project target. android.library.reference.1=../appcompat_v7 target=android-19 ...ex09:网络访问 HTTP get/post WebService ex10:网络服务 使用Ksoap2来访问WebSerivce
基于发布订阅(Pub/Sub)模式的一个事件消息库,使用通用的 register(target), unregister(target), post(event) 消息通信接口,能有效的减少甚至消除Android应用中异步任务逻辑和界面更新之间的耦合,实现模块化,...
retrofit 简单封装 实现公共参数get和post统一处理,上传和下载带进度显示,图片和本文一起提交
利用注解实现事件订阅执行方法 tag 用于细化区分事件 mode 方法执行线程 @Subscriber(tag = DEFAULT_TAG, mode = ThreadMode.MAIN) private fun change2(event: XXXEvent){ textView2.text = "今
图片下载(实现了图片的压缩) 使用示例 GET请求 String url = "https://www.baidu.com/"; UrlHttpUtil.get(url, new CallBackUtil.CallBackString() { @Override public void onFailure(int code, String error...
实现android的异步http请求,可以使用post和get方式
Medium Post: https://goo.gl/7oH1B1 Getting Started Project root build.gradle allprojects { repositories { maven { url 'https://jitpack.io' } google() } } Module build.gradle dependencies { ...
主要是数据post、get等请求工具类,所有请求调用doHttp()方法。 2、RetrofitFileUtils.java 主要用于文件下载工具类,包含uploadFile()文件上传、downloadFile()文件下载两个方法。 3、...
这是一个专用于解决Android中网络请求及图片加载的缓存处理框架 项目目标 本项目是作为实验项目,不保证其稳定性及可靠性 因为缓存业务的复杂性,本项目尽可能适应更多的使用场景 目前考虑到的,会实现的功能清单,...
android实现登陆和注册的前后台框架,利用http的post和get进行请求,请求方式已经封装完善.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
Http通信又分为两种方式:get和post,get可以uoqu静态页面,传入参数可以放在url当中,而post方法的传入参数则是放在httprequest(请求)当中。前面提到的HttpUrlConnection接口是java当中的通信接口,而HttpClient...