package com.example.lesson09_login; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; 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 android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.TextUtils; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; @SuppressLint("HandlerLeak") public class MainActivity extends Activity { public Button btn_get, btn_post; public EditText et_name, et_pass; public NetWorkUtil netWorkUtil; public Handler handler; public static final int GET = 1; public static final int POST = 2; public String name, pass, content; public String path = "http://172.22.64.18:8080/lesson09_login/csdn/UserAction_login.action"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_get = (Button) findViewById(R.id.btn_get); btn_post = (Button) findViewById(R.id.btn_post); et_name = (EditText) findViewById(R.id.et_name); et_pass = (EditText) findViewById(R.id.et_pass); netWorkUtil = new NetWorkUtil(this); handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case GET: Toast.makeText(MainActivity.this, "GET请求成功++++++++++", Toast.LENGTH_LONG).show(); Log.v("MainActity", content); break; case POST: Toast.makeText(MainActivity.this, "POST请求成功----------", Toast.LENGTH_LONG).show(); Log.v("MainActity", content); break; default: break; } } }; } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void sendGet(View v) { if (!isEmpty()) { Toast.makeText(MainActivity.this, "用户名或密码不能为空...", Toast.LENGTH_LONG).show(); } else { boolean flag = netWorkUtil.setNetWork(); if (flag) { new Thread(new Runnable() { @Override public void run() { name = et_name.getText().toString(); pass = et_pass.getText().toString(); // sendGetRequest(path, name, pass); sendGetClient(path, name, pass); handler.sendEmptyMessage(GET); } }).start(); } } } public void sendPost(View v) { if (!isEmpty()) { Toast.makeText(MainActivity.this, "用户名或密码不能为空...", Toast.LENGTH_LONG).show(); } else { boolean flag = netWorkUtil.setNetWork(); if (flag) { new Thread(new Runnable() { @Override public void run() { name = et_name.getText().toString(); pass = et_pass.getText().toString(); // sendPostRequest(path, name, pass); sendPostClient(path, name, pass); handler.sendEmptyMessage(POST); } }).start(); } } } public boolean isEmpty() { boolean flag = false; if (TextUtils.isEmpty(et_name.getText().toString()) || TextUtils.isEmpty(et_pass.getText().toString())) { flag = false; } else { flag = true; } return flag; } @SuppressWarnings("unused") private void sendGetRequest(String path, String name, String pass) { String str = path + "?user.name=" + name + "&user.pass=" + pass; try { URL url = new URL(str); HttpURLConnection httpURLConnection = (HttpURLConnection) url .openConnection(); httpURLConnection.setConnectTimeout(5000); httpURLConnection.setRequestMethod("GET"); if (httpURLConnection.getResponseCode() == 200) { InputStream is = httpURLConnection.getInputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int len = 0; while ((len = is.read(buf)) != -1) { bos.write(buf, 0, len); } byte[] data = bos.toByteArray(); bos.flush(); bos.close(); is.close(); content = new String(data); if (content.contains("gb2312")) { content = new String(data, "gb2312"); } } httpURLConnection.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (ProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @SuppressWarnings("unused") private void sendPostRequest(String path, String name, String pass) { try { String data = "user.name=" + name + "&user.pass=" + pass; URL url = new URL(path); HttpURLConnection httpURLConnection = (HttpURLConnection) url .openConnection(); httpURLConnection.setConnectTimeout(5000); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setDoOutput(true); httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); httpURLConnection.setRequestProperty("Content-Length", data.length() + ""); OutputStream os = httpURLConnection.getOutputStream(); byte[] buf = data.getBytes(); os.write(buf); os.flush(); os.close(); if (httpURLConnection.getResponseCode() == 200) { InputStream is = httpURLConnection.getInputStream(); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf1 = new byte[1024]; int len = 0; while ((len = is.read(buf1)) != -1) { bos.write(buf1, 0, len); } byte[] data1 = bos.toByteArray(); bos.flush(); bos.close(); is.close(); content = new String(data1); } httpURLConnection.disconnect(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void sendGetClient(String path, String name, String pass) { try { String uri = path + "?user.name=" + URLEncoder.encode(name, "UTF-8") + "&user.pass=" + URLEncoder.encode(pass, "UTF-8"); HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(uri); HttpResponse httpResponse = httpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode() == 200) { InputStream is = httpResponse.getEntity().getContent(); content = StringTool.isContent(is); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @SuppressWarnings("unused") public void sendPostClient(String path, String name, String pass) { HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(path); List<BasicNameValuePair> parameters = new ArrayList<BasicNameValuePair>(); parameters.add(new BasicNameValuePair("user.name", name)); parameters.add(new BasicNameValuePair("user.pass", pass)); UrlEncodedFormEntity entity = null; try { entity = new UrlEncodedFormEntity(parameters, "UTF-8"); HttpResponse httpResponse = httpClient.execute(httpPost); if (httpResponse.getStatusLine().getStatusCode() == 200) { InputStream is = httpResponse.getEntity().getContent(); content = StringTool.isContent(is); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
package com.example.lesson09_login; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; /** * 2013-6-10 上午9:33:34 * * @author 乔晓松 */ public class StringTool { public static String isContent(InputStream is) { String content = null; try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; int len = 0; while ((len = is.read(buf)) != -1) { bos.write(buf, 0, len); } byte[] data = bos.toByteArray(); bos.flush(); bos.close(); is.close(); content = new String(data); } catch (IOException e) { e.printStackTrace(); } return content; } }
此代码都是本人亲自测试成功的,如要源码请给我留言