本帖最後由 george310909 於 2012-3-21 21:35 編輯
喔,原來你是要跟網頁結合,那不好意思,我誤會了
- package test.gl;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.ClientProtocolException;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- 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 java.io.IOException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- public class testgl extends Activity {
- private Button mButton1;
- private TextView mTextView1;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mButton1 = (Button) findViewById(R.id.myButton1);
- mTextView1 = (TextView) findViewById(R.id.myTextView1);
- mButton1.setOnClickListener(new Button.OnClickListener() {
- @Override
- public void onClick(View v) {
- String uriAPI = "http://www.google.com.tw";
- HttpPost httpRequest = new HttpPost(uriAPI);
- List<NameValuePair> params = new ArrayList<NameValuePair>();
- params.add(new BasicNameValuePair("str", "I am Post String"));
- try {
- httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
- HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
- if (httpResponse.getStatusLine().getStatusCode() == 200) {
- //接收原始碼的變數
- String strResult = EntityUtils.toString(httpResponse.getEntity());
- mTextView1.setText(strResult);
- } else {
- mTextView1.setText("Error Response: "+ httpResponse.getStatusLine().toString());
- }
- } catch (ClientProtocolException e) {
- mTextView1.setText(e.getMessage().toString());
- e.printStackTrace();
- } catch (IOException e) {
- mTextView1.setText(e.getMessage().toString());
- e.printStackTrace();
- } catch (Exception e) {
- mTextView1.setText(e.getMessage().toString());
- e.printStackTrace();
- }
- }
- });
- }
- public String eregi_replace(String strFrom, String strTo, String strTarget) {
- String strPattern = "()" + strFrom;
- Pattern p = Pattern.compile(strPattern);
- Matcher m = p.matcher(strTarget);
- if (m.find()) {
- return strTarget.replaceAll(strFrom, strTo);
- } else {
- return strTarget;
- }
- }
- }
複製代碼 以前做的
想當年應付指導教授很好用 |