關於HTTP傳輸的介紹請見http://blog.csdn.net/theworldsong/article/details/9107789。
以下例子分別以POST和GET方式向網站傳輸數據,並返回數據將其顯示。
package dfzy.EX088;
/*必需引用apache.http相關類來建立HTTP聯機*/
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.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 dfzy.EX088.R;
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 EX088 extends Activity
{
private Button mButton1,mButton2;
private TextView mTextView1;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton1 =(Button) findViewById(R.id.myButton1);
mButton2 =(Button) findViewById(R.id.myButton2);
mTextView1 = (TextView) findViewById(R.id.myTextView1);
mButton1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
//聲明地址
String uriAPI = "http://www.dubblogs.cc:8751/Android/Test/API/Post/index.php";
//建立HTTP Post聯機
HttpPost httpRequest = new HttpPost(uriAPI);
//Post運行傳送變量必須用NameValuePair[]儲存,這是一種封裝,專門用於HTTP的POST傳輸
List <NameValuePair> params = new ArrayList <NameValuePair>();
params.add(new BasicNameValuePair("str", "I am Post String"));
try
{
//將要附帶的數據包放入,並聲明爲UTF_8字符格式
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
//發送後,會等待返回一個HttpResponse
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
//若狀態碼爲200
if(httpResponse.getStatusLine().getStatusCode() == 200)
{
/*獲取字符串*/
String strResult = EntityUtils.toString(httpResponse.getEntity());
mTextView1.setText(strResult);
}
else
{
mTextView1.setText("獲取失敗");
}
}
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();
}
}
});
mButton2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
String uriAPI = "http://www.dubblogs.cc:8751/Android/Test/API/Post/index.php?str=I+am+Get+String";
/*建立HTTP Get聯機*/
HttpGet httpRequest = new HttpGet(uriAPI);
try
{
/*發送獲取的HTTP request,並等待返回一個HttpResponse*/
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
/*若狀態碼爲200*/
if(httpResponse.getStatusLine().getStatusCode() == 200)
{
//獲取字符串
String strResult = EntityUtils.toString(httpResponse.getEntity());
mTextView1.setText(strResult);
}
else
{
mTextView1.setText("獲取失敗");
}
}
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();
}
}
});
}
}
代碼中使用的網站可能失效,但是明白道理就行,就是返回一串字符。
另參考
http://52android.blog.51cto.com/2554429/496621
轉載請註明來源,版權歸原作者所有,未經同意嚴禁用於任何商業用途。
微博:http://weibo.com/theworldsong
郵箱:[email protected]