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
原创粉丝点击