package net.blogjava.mobile; import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List; import org.apache.http.HttpResponse;
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.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 Main extends Activity implements OnClickListener
{ @Override
public void onClick(View view)
{
String url = "http://169.254.103.188/querybooks/QueryServlet";
TextView tvQueryResult = (TextView) findViewById(R.id.tvQueryResult);
EditText etBookName = (EditText) findViewById(R.id.etBookName);
HttpResponse httpResponse = null;
try
{ switch (view.getId())
{
case R.id.btnGetQuery:
url += "?bookname=" + etBookName.getText().toString();
HttpGet httpGet = new HttpGet(url);
httpResponse = new DefaultHttpClient().execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200)
{ String result = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8);
tvQueryResult.setText(result);
System.out.println(result);
}
break; case R.id.btnPostQuery:
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("bookname", etBookName
.getText().toString()));
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); httpResponse = new DefaultHttpClient().execute(httpPost);
if (httpResponse.getStatusLine().getStatusCode() == 200)
{
String result = EntityUtils.toString(httpResponse
.getEntity());
tvQueryResult.setText(result.replaceAll("\r", ""));
}
break;
}
}
catch (Exception e)
{
tvQueryResult.setText(e.getMessage());
} } @Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnGetQuery = (Button) findViewById(R.id.btnGetQuery);
Button btnPostQuery = (Button) findViewById(R.id.btnPostQuery);
btnGetQuery.setOnClickListener(this);
btnPostQuery.setOnClickListener(this); }
}
上传文件时,可以使用HttpAnalyzer来捕获HTTP请求信息:上传文档新建文本文档 (3).txt过程:
Request Headers Value
(Request-Line) POST /upload/UploadServlet HTTP/1.1
Accept text/html, application/xhtml+xml, */*
Referer http://localhost/upload/upload.jsp
Accept-Language zh-CN
User-Agent Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)
Content-Type multipart/form-data; boundary=---------------------------7dea05100618
Accept-Encoding gzip, deflate
Host localhost
Content-Length 239
DNT 1
Connection Keep-Alive
Cache-Control no-cache
Cookie JSESSIONID=C55149A1C7C5EEFE0A67D4DA2183E8BF
Response Headers Value
(Status-Line) HTTP/1.1 200 OK
Server Apache-Coyote/1.1
Content-Type text/html;charset=UTF-8
Content-Length 21
Date Sun, 14 Dec 2014 07:00:05 GMT
stream标签页信息:
744 bytes sent to :0
POST /upload/UploadServlet HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Referer: http://localhost/upload/upload.jsp
Accept-Language: zh-CN
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)
Content-Type: multipart/form-data; boundary=---------------------------7dea05100618
Accept-Encoding: gzip, deflate
Host: localhost
Content-Length: 239
DNT: 1
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: JSESSIONID=C55149A1C7C5EEFE0A67D4DA2183E8BF -----------------------------7dea05100618
Content-Disposition: form-data; name="file"; filename="C:\Users\Administrator\Desktop\鏂板缓鏂囨湰鏂囨。 (3).txt"
Content-Type: text/plain 123abc
-----------------------------7dea05100618--
163 bytes received by :0
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Content-Length: 21
Date: Sun, 14 Dec 2014 07:00:05 GMT 鏂囦欢涓婁紶鎴愬姛!