android http 通信实例(未能正常运行,待后续完善)
2015-12-14 00:00
656 查看
主布局文件:
主活动类HttpMainActivity.java:
添加访问Internet的权限:
<uses-permission android:name="android.permission.INTERNET"/>
<?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="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="这是一个HTTP通信方式的示例"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="通过get方式获取数据" android:id="@+id/bt_get"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="通过post方式获取数据" android:id="@+id/bt_post"/> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tv_content"/> </LinearLayout>
主活动类HttpMainActivity.java:
package com.example.ch10; 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.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 com.example.baseexample.R; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class HttpMainActivity extends Activity { private TextView tv_content; private Button bt_get,bt_post; public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.ch10_httpmain); tv_content = (TextView)findViewById(R.id.tv_content); bt_get = (Button)findViewById(R.id.bt_get); bt_post = (Button)findViewById(R.id.bt_post); bt_get.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { try{ final String httpUrl = "http://192.168.1.100:8081"; HttpGet httpRequest = new HttpGet(httpUrl); HttpClient httpClient = new DefaultHttpClient(); HttpResponse httpResponse = httpClient.execute(httpRequest); if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){ String strResult = EntityUtils.toString(httpResponse.getEntity()); tv_content.setText(strResult); }else{ tv_content.setText("请求错误"); } }catch(Exception e){ Toast.makeText(HttpMainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show(); } } }); bt_post.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { try{ final String httpUrl = "http://10.0.2.2:8080/WebDemo/UI/httptest.jsp"; HttpPost httpRequest = new HttpPost(httpUrl); List<NameValuePair> param = new ArrayList<NameValuePair>(); param.add(new BasicNameValuePair("par", "Post Type:abcdef")); HttpEntity entity = new UrlEncodedFormEntity(param,"utf-8"); httpRequest.setEntity(entity); HttpClient httpClient = new DefaultHttpClient(); HttpResponse httpResponse = httpClient.execute(httpRequest); if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){ String strResult = EntityUtils.toString(httpResponse.getEntity()); tv_content.setText(strResult); }else{ tv_content.setText("请求错误"); } }catch(Exception e){ Toast.makeText(HttpMainActivity.this, e.getMessage().toString(), Toast.LENGTH_LONG).show(); } } }); } }
添加访问Internet的权限:
<uses-permission android:name="android.permission.INTERNET"/>
相关文章推荐
- 在tcp/ip协议中以BigEndian方式的编码与解码
- http下载文件
- 自定义的投票协议的通讯(tcp/ip 与 udp示例)
- 厦门巨游网络科技有限公司(HOTPOWER)承接游戏UI外包
- HTTP Request header
- java TCP和UDP 连接
- CXF客户端调用https Webservice
- Nginx的https设置
- fork http://coolshell.cn/articles/7965.html
- HttpClient第一章(三)
- 【转载】HTTP状态码的来龙去脉
- 认识 HTTP 请求
- TCPIP详解 卷一核心概念整理
- 神经网络-激活函数
- Http协议的常见参数
- TCP/IP详解卷1 读书笔记:HTTP协议
- 新浪自媒体重新开放注册 一点资讯全面布局?
- HTTP协议简略
- TCP和UDP的区别
- Android基础的sharedPreferences存储,ListView,以及httpclient,和asynchttp框架请求的使用