Android网络编程:通过Http与服务器交互
来源:互联网 发布:淘宝层级怎么刷 编辑:程序博客网 时间:2024/06/04 08:55
Http通信中的POST和GET请求方式的不同。GET可以获得静态页面,也可以把参数放在URL字符串后面,传递给服务器。而POST方法的参数是放在Http请求中。因此,在编程之前,应当首先明确使用的请求方法,然后再根据所使用的方式选择相应的编程方式。
如果服务器中使用Apache则Android可使用HttpClient接口
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"android:orientation="vertical"> <EditText android:id="@+id/editText2" android:layout_width="match_parent" android:layout_height="wrap_content" android:editable="false" android:cursorVisible="false"/> <Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="点击通过POST获取HTTP连接" /> <ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content" > <EditText android:id="@+id/editText1" android:layout_width="fill_parent" android:layout_height="match_parent" android:editable="false" android:cursorVisible="false"/> </ScrollView> <Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="点击通过GET获取HTTP连接" /></LinearLayout>
package com.example.httptext;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpResponse;import org.apache.http.HttpStatus;import org.apache.http.NameValuePair;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.protocol.HTTP;import org.apache.http.util.EntityUtils;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;public class MainActivity extends Activity {String urlPost = "http://115.28.143.212:8899/MyHttpSample/response.jsp";String urlGet = "http://www.baidu.com";Button btnPost = null;Button btnGET = null;EditText etPost = null;EditText etGet = null;String postResult = null;String getResult = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btnPost = (Button) findViewById(R.id.button1);btnGET = (Button) findViewById(R.id.button2);etPost = (EditText) findViewById(R.id.editText2);etGet = (EditText) findViewById(R.id.editText1);btnPost.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {new Thread(){@Overridepublic void run() {httpPost();httpGet();handler.sendEmptyMessage(0);}}.start();}});}private Handler handler = new Handler(){@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubsuper.handleMessage(msg);etPost.setText(postResult);etGet.setText(getResult);}};public void httpPost(){HttpPost httpPostRequest = new HttpPost(urlPost);//创建HttpPost对象List<NameValuePair> httpParams = new ArrayList<NameValuePair>();//创建存放参数的ArrayListhttpParams.add(new BasicNameValuePair("name","Java"));//设置post参数try{httpPostRequest.setEntity(new UrlEncodedFormEntity(httpParams,HTTP.UTF_8));//创建一个DefaultHttpClient对象,并令其执行设置好的HttpPost请求。HttpResponse httpResponse = new DefaultHttpClient().execute(httpPostRequest);if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){postResult = EntityUtils.toString(httpResponse.getEntity());postResult = postResult.replace("\r\n|\n\r|\r|\n", "");//去掉信息中的回车和换行}}catch(Exception e){e.printStackTrace();}}public void httpGet(){HttpGet htttpGetRequest =new HttpGet(urlGet);try{HttpResponse httpResponse = new DefaultHttpClient().execute(htttpGetRequest);if(httpResponse.getStatusLine().getStatusCode()==HttpStatus.SC_OK){getResult = EntityUtils.toString(httpResponse.getEntity());getResult = getResult.replace("\r\n|\n\r|\r|\n", "");}}catch(Exception e){e.printStackTrace();}}}
0 0
- Android网络编程:通过Http与服务器交互
- Android通过http与服务器端交互
- android开发之Http与服务器交互
- android利用网络与服务器进行交互
- Android中网络编程以及与服务器上Web项目的基础交互
- android操作通过http与服务器通信
- 基于HTTP的客户端与服务器交互编程
- Android客户端与PC服务器通过socket进行交互实例
- Android客户端与PC服务器通过socket进行交互实例
- android客户端通过webServcie与服务器交互,如何保持Session
- Android客户端与PC服务器通过socket进行交互实例
- Android客户端与PC服务器通过socket进行交互实例
- android网络编程 -- HTTP通信(01)TOMCAT HTTP服务器搭建
- android客户端 与服务器交互 http
- HTTP服务器与android客户端的json交互
- android-async-http框架之与服务器进行数据交互
- Android服务器与客户端数据交互(Http协议)
- HTTP服务器与Android客户端的json交互
- Hibernate异常:java.lang.NoClassDefFoundError: org/objectweb/asm/Type
- 表分区简介
- rman恢复spfile
- start.S的深入理解和分析
- error: revision refs/heads/master in manifests not found
- Android网络编程:通过Http与服务器交互
- Linux - vnc 重启失败
- [2119]数据结构实验之链表四:有序链表的归并 (SDUT)
- linux socket编程(udev举例)
- 设备树使用手册
- mysql中列信息查询(information_schema.columns )
- php获取文件mime类型的几种方法
- iOS8测试版轻松安装
- C中堆管理——浅谈malloc,calloc,realloc函数之间的区别