Android's http two request methods

Two request methods of http: POST and GET

. Since the Android SDK includes the org.apache.http package, there is no need to import the jar. The

GET method is:

String serverURL = "http://127.0.0.1/xxx/xx.jsp?username= abc;
HttpGet httpRequest = new HttpGet(serverURL);// Establish http get online
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);// Send http request
if (httpResponse.getStatusLine().getStatusCode() == 200)
    String result = EntityUtils.toString(httpResponse.getEntity());// Get the corresponding string
POST method:

copy code
        String uriAPI = "http://127.0.0.1/xxx/xx.jsp"; //Declare URL characters String
        HttpPost httpRequest = new HttpPost(uriAPI); //Create HTTP POST connection
        List <NameValuePair> params = new ArrayList <NameValuePair>(); //Post operation transmission variables must be stored in NameValuePair[] array
        params.add(new BasicNameValuePair("str", "I am Post String"));  
        httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));   //发出http请求
        HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);   //取得http响应
        if(httpResponse.getStatusLine().getStatusCode() == 200)   
          String strResult = EntityUtils.toString(httpResponse.getEntity());   //获取字符串
复制代码

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326522321&siteId=291194637