HttpClient接口Post方式
生活随笔
收集整理的這篇文章主要介紹了
HttpClient接口Post方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; 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.util.EntityUtils; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.TextView;public class mainActivity extends Activity {TextView tv;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);tv = (TextView) findViewById(R.id.tv1);// HttpPost連接對象HttpPost httpRequset = new HttpPost("http://10.0.2.2:8080/dem/");// 使用NameValuePair來保存要傳遞的Post參數List<NameValuePair> params = new ArrayList<NameValuePair>();// 添加要傳遞的參數params.add(new BasicNameValuePair("par", "asdf"));try {// 設置字符集HttpEntity httpentity = new UrlEncodedFormEntity(params, "gb2312");// 請求httpRequsethttpRequset.setEntity(httpentity);// 取得HttpClientHttpClient httpClient = new DefaultHttpClient();// 取得HttpResponseHttpResponse httpResponse = httpClient.execute(httpRequset);if(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {String strResult = EntityUtils.toString(httpResponse.getEntity());tv.setText(strResult);}else {tv.setText("請求錯誤");}}catch(Exception e) {Log.e("", e.getMessage());}} }版權聲明:本文為博主原創文章,未經博主允許不得轉載。
轉載于:https://my.oschina.net/u/2406628/blog/473359
總結
以上是生活随笔為你收集整理的HttpClient接口Post方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 新手!SDK Manager里找不到AP
- 下一篇: ECshop网点程序优化-后台添加类目自