POST設置提交參數代碼:
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name","張三"));
//訪問Intent網路 記得註冊事件
<uses-permission android:name="android.permission.INTERNET" />

 

POST、GET 類

 

public class GetPostDao {
private Context context;
//get post 的參數名都是 name=?
public static final String URL="http://192.168.1.27/index.php";
public GetPostDao(Context context){
this.context=context;

 

 

 

}
public String toPost(List<NameValuePair> params){
HttpClient httpclient = new DefaultHttpClient();
//創建HttpPost物件
HttpPost httppost = new HttpPost(URL);
try
{
//設置httpPost請求參數
httppost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
//使用execute方法發送HTTP Post請求,並返回HttpResponse物件
HttpResponse httpResponse = httpclient.execute(httppost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if(statusCode==HttpStatus.SC_OK)
{
//獲得返回結果
return EntityUtils.toString(httpResponse.getEntity());
}
}catch (Exception e)
{
e.printStackTrace();
}
return null;
}
public String toGet(String url){
HttpClient httpclient = new DefaultHttpClient();
System.out.println(URL+"?name="+url);
//創建HttpGet物件
HttpGet httpGet = new HttpGet(URL+"?name="+url);

 

//使用execute方法發送HTTP GET請求,並返回HttpResponse物件

 

try {
HttpResponse httpResponse = httpclient.execute(httpGet);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if(statusCode==HttpStatus.SC_OK){
//獲得返回結果
String str=EntityUtils.toString(httpResponse.getEntity());
return str;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}

 

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()