鱼C论坛

 找回密码
 立即注册
查看: 2447|回复: 0

[学习笔记] android 9.2.1

[复制链接]
发表于 2017-11-1 22:22:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
新建项目,修改activity_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:orientation="vertical">

  6.     <Button
  7.         android:id="@+id/send_request"
  8.         android:layout_width="match_parent"
  9.         android:layout_height="wrap_content"
  10.         android:text="send_request"/>
  11.     <ScrollView
  12.         android:layout_width="match_parent"
  13.         android:layout_height="match_parent">
  14.         <TextView
  15.             android:id="@+id/response_text"
  16.             android:layout_width="match_parent"
  17.             android:layout_height="wrap_content" />
  18.     </ScrollView>

  19. </LinearLayout>
复制代码

AndroidManifest.xml的manifest标签下添加
  1. <uses-permission android:name="android.permission.INTERNET"/>
复制代码

修改mainactivity.java
  1. package com.example.xinwei.networktest;

  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.TextView;

  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.io.InputStreamReader;
  12. import java.io.StringReader;
  13. import java.net.HttpURLConnection;
  14. import java.net.MalformedURLException;
  15. import java.net.URL;
  16. import java.net.URLDecoder;
  17. import java.net.URLEncoder;

  18. public class MainActivity extends AppCompatActivity implements View.OnClickListener{

  19.     TextView responseText;
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.activity_main);
  24.         Button sendRequest = (Button)findViewById(R.id.send_request);
  25.         responseText = (TextView)findViewById(R.id.response_text);
  26.         sendRequest.setOnClickListener(this);
  27.     }

  28.     @Override
  29.     public void onClick(View view) {
  30.         if (view.getId()==R.id.send_request){
  31.             sendRequestWithHttpURLConnection();
  32.         }
  33.     }

  34.     private void sendRequestWithHttpURLConnection() {
  35.         new Thread() {
  36.             @Override
  37.             public void run() {
  38.                 HttpURLConnection connection = null;
  39.                 BufferedReader reader=null;
  40.                 try {
  41.                     URL url = new URL("http://www.fishC.com");
  42.                     connection = (HttpURLConnection)url.openConnection();
  43.                     connection.setRequestMethod("GET");
  44.                     connection.setRequestProperty("Content-type", "application/x-java-serialized-object");
  45.                     Log.d("MainActivity", ""+connection.getResponseCode());
  46.                     connection.setConnectTimeout(8000);
  47.                     connection.setReadTimeout(8000);
  48.                     InputStream in = connection.getInputStream();
  49.                     reader=new BufferedReader(new InputStreamReader(in));
  50.                     StringBuilder response=new StringBuilder();
  51.                     String line;
  52.                     while ((line=reader.readLine())!=null){
  53.                         response.append(line);
  54.                     }
  55.                     showResponse(response.toString());
  56.                 } catch (MalformedURLException e) {
  57.                     e.printStackTrace();
  58.                 } catch (IOException e) {
  59.                     e.printStackTrace();
  60.                 }finally {
  61.                     if(reader!=null){
  62.                         try {
  63.                             reader.close();
  64.                         } catch (IOException e) {
  65.                             e.printStackTrace();
  66.                         }
  67.                     }
  68.                     if (connection!=null){
  69.                         connection.disconnect();
  70.                     }
  71.                 }

  72.             }

  73.         }.start();
  74.     }

  75.     private void showResponse(final String response) {
  76.         runOnUiThread(new Runnable() {
  77.             @Override
  78.             public void run() {
  79.                 responseText.setText(response);
  80.             }
  81.         });
  82.     }
  83. }
复制代码

这里我把书上的baidu地址改成了鱼C,因为百度网老是重定向,头疼啊不知道怎么弄
效果图:
asdsaad.png

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 10:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表