温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

http请求方式httpURLContention和httpClient

发布时间:2020-07-31 19:29:22 来源:网络 阅读:256 作者:lg491733638 栏目:移动开发
package com.example.http; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.annotation.SuppressLint; import android.app.Activity; @SuppressLint("HandlerLeak") public class MainActivity extends Activity {  private static final int SHOW_RESPONSE = 0;  private TextView textView;  private Button button;  private Handler handler = new Handler() {   public void handleMessage(Message msg) {    switch (msg.what) {    case SHOW_RESPONSE:     String response = (String) msg.obj;     textView.setText(response);     break;    }   }  };  @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.activity_main);   textView = (TextView) findViewById(R.id.webview);   button = (Button) findViewById(R.id.button);   button.setOnClickListener(new OnClickListener() {    @Override    public void onClick(View v) {     // sendRequestWithHttpConnection();     sendRequestWithHttpClient();    }   });  }  /**   * httpclient网络访问   */  protected void sendRequestWithHttpClient() {   new Thread(new Runnable() {    @Override    public void run() {     try {      HttpClient httpClient = new DefaultHttpClient();      HttpGet httpGet = new HttpGet("http://www.baidu.com");      HttpResponse httpResponse = httpClient.execute(httpGet);      if (httpResponse.getStatusLine().getStatusCode() == 200) {       // 请求成功       HttpEntity entity = httpResponse.getEntity();       String response = EntityUtils.toString(entity, "utf-8");       Message message = new Message();       message.what = SHOW_RESPONSE;       message.obj = response.toString();       handler.sendMessage(message);      }     } catch (Exception e) {      e.printStackTrace();     } finally {     }    }   }).start();  }  /**   * httpURLConnection网络访问   */  protected void sendRequestWithHttpConnection() {   new Thread(new Runnable() {    @Override    public void run() {     HttpURLConnection connection = null;     try {      URL url = new URL("http://www.baidu.com");      connection = (HttpURLConnection) url.openConnection();      connection.setRequestMethod("GET");      connection.setConnectTimeout(8000);      connection.setReadTimeout(8000);      connection.setDoInput(true);      connection.setDoOutput(true);      InputStream inputStream = connection.getInputStream();      BufferedReader reader = new BufferedReader(        new InputStreamReader(inputStream));      StringBuffer response = new StringBuffer();      String line;      while ((line = reader.readLine()) != null) {       response.append(line);      }      Message message = new Message();      message.what = SHOW_RESPONSE;      message.obj = response.toString();      handler.sendMessage(message);     } catch (Exception e) {      e.printStackTrace();     } finally {      if (connection != null) {       connection.disconnect();      }     }    }   }).start();  } } 布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"      android:orientation="vertical"     >          <Button          android:id="@+id/button"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="send request"         />               <ScrollView         android:layout_width="match_parent"         android:layout_height="wrap_content" >         <TextView             android:id="@+id/webview"             android:layout_width="match_parent"             android:layout_height="wrap_content" />     </ScrollView> </LinearLayout>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI