package my.android.hc; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; 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.widget.Button; import android.widget.TextView; public class HttpConnect extends Activity { private Button mb1,mb2; private TextView mtv1; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mb1 = (Button) findViewById(R.id.b1_id); mb2 = (Button) findViewById(R.id.b2_id); mtv1 = (TextView) findViewById(R.id.tv1_id); mb1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub String uriAPI = "http://www.dubblogs.cc:8751/android/test/api/post/index.php"; 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()); mtv1.setText(strResult); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } } ); mb2.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub String uriAPI = "http://www.dubblogs.cc:8751/android/test/api/get/index.php?str=I+am+Get+String"; HttpGet httpRequest = new HttpGet(uriAPI); try { HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest); if (httpResponse.getStatusLine().getStatusCode() == 200) { String strResult = EntityUtils.toString(httpResponse.getEntity()); strResult = eregi_replace("(/r/n|/r|/n|/n/r)", "", strResult); mtv1.setText(strResult); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } } ); } protected String eregi_replace(String strFrom, String strTo, String strTarget) { // TODO Auto-generated method stub String strPattern = "(?!)" + strFrom; Pattern p = Pattern.compile(strPattern); Matcher m = p.matcher(strTarget); if (m.find()) { return strTarget.replaceAll(strFrom, strTo); } else { return strTarget; } } } <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/tv1_id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/b1_id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/b1" /> <Button android:id="@+id/b2_id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/b2" /> </LinearLayout> <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, HttpConnect!</string> <string name="app_name">HttpConnect</string> <string name="b1">button1</string> <string name="b2">button2</string> </resources> <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="my.android.hc" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".HttpConnect" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.INTERNET"> </uses-permission> </manifest>