Android URLconnection

使用Thread请求HTTP

普通的使用java的Thread 线程,在重写run 方法,实现HTTPConnection

public class DoubleMeActivity extends Activity implements OnClickListener {

    EditText inputValue=null;
    Integer doubledValue =0;
    Button doubleMe;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.calculate);

        inputValue = (EditText) findViewById(R.id.inputNum);
        doubleMe = (Button) findViewById(R.id.doubleme);

        doubleMe.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
        case R.id.doubleme:

              new Thread(new Runnable() {
                    public void run() {

                        try{
                            URL url = new URL("http://10.0.2.2:8080/MyServletProject/DoubleMeServlet");
                            URLConnection connection = url.openConnection();

                            String inputString = inputValue.getText().toString();
                            //inputString = URLEncoder.encode(inputString, "UTF-8");

                            Log.d("inputString", inputString);

                            connection.setDoOutput(true);
                            OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
                            out.write(inputString);
                            out.close();

                            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

                            String returnString="";
                            doubledValue =0;

                            while ((returnString = in.readLine()) != null)
                            {
                                doubledValue= Integer.parseInt(returnString);
                            }
                            in.close();

                            runOnUiThread(new Runnable() {
                                 public void run() {

                                     inputValue.setText(doubledValue.toString());

                                }
                            });

                            }catch(Exception e)
                            {
                                Log.d("Exception",e.toString());
                            }

                    }
                  }).start();

            break;
            }
        }

    }
时间: 2025-01-07 00:57:27

Android URLconnection的相关文章

Android URLConnection发送Get请求 HttpGet封装

一.使用URLConnection发送Get请求 1.与服务器建立连接: URLConnection connection=new URL("https://www.baidu.com/").openConnection(); 2.设置请求头(Cookie亦可通过请求头设置): connection.setRequestProperty("Referer","https://www.baidu.com/"); connection.setRequ

Android中的网络编程系列(一):URLConnection

转载请注明出处:http://blog.csdn.net/bettarwang/article/details/41229955 URL(Uniform Resource Locator)对象代表统一资源定位器,它是指向互联网资源的指针.URL由协议名.主机.端口和资源路径组件,即满足如下格式: protocol://host:port/path 例如http://kan.sogou.com/dianying/就是一个URL地址. URL提供了多个构造方法用于创建URL对象,同时它提供的主要方法

android 中HttpClient和URLConnection的区别

分析一: 在研究Volley框架的源码中,发现它在HTTP请求的使用上比较有意思,在Android 2.3及以上版本,使用的是HttpURLConnection,而在Android 2.2及以下版本,使用的是HttpClient.我也比较好奇这么使用的原因,于是专门找到了一位Google的工程师写的一篇博客,文中对HttpURLConnection和HttpClient进行了对比,下面我就给大家简要地翻译一下. 原文地址:http://android-developers.blogspot.co

Android 网络编程(3)——使用URLConnection提交请求

URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和URL之间的通信连接.程序可以通过URLConnection实例向该URL发送请求,读取URL引用的资源 通常创建一个和URL的连接,并发送请求.读取此URL引用的资源需要如下几个步骤: 通过调用URL对象openConnection()方法来创建URLConnection对象 设置URLConnection的参数和普通请求属性 如果只是发送GET方法请求,使用connect方式建立和远程资

【Android自学日记】关于Bitmap的理解和使用-不完整版

最近的Android自学刚好学习到异步线程的使用,对于开启异步线程加载网络图片中用到的Bitmap有点小蒙逼,这到底是个啥???所以我就自信的打开了百度!!以下就是我学习到的知识! 百度定义: 位图文件(Bitmap),扩展名可以是.bmp或者.dib.位图是Windows标准格式图形文件,它将图像定义为由点(像素)组成,每个点可以由多种色彩表示,包括2.4.8.16.24和32位色彩.例如,一幅1024×768分辨率的32位真彩图片,其所占存储字节数为:1024×768×32/(8*1024)

android 之post,get方式请求数据

get方式和post方式的区别: 1.请求的URL地址不同: post:"http://xx:8080//servlet/LoginServlet" get:http://xxx:8080//servlet/LoginServlet?username=root&pwd=123 2.请求头不同: ****post方式多了几个请求头:Content-Length   ,   Cache-Control , Origin openConnection.setRequestProper

【黑马Android】(06)使用HttpClient方式请求网络/网易新闻案例

使用HttpClient方式请求网络 <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 19 activity纵横屏切换的数据保存与恢复

Bundle类:竖屏的activity换到横屏的activity的时候,会把竖屏的activity杀掉横屏的activity创建,竖屏的activity会有一些计算结果,可以用数据存起来,存到内存里面,然后存在横屏的activity. Intent类putExtra("user", user);传数据的时候用的就是Bundle对象, private Bundle mExtras; Bundle是一个map, package com.sxt.day04_03; import java.i

Android网络:HTTP之利用HttpURLConnection访问网页、获取网络图片实例 (附源码)

http://blog.csdn.net/yanzi1225627/article/details/22222735 如前文所示的TCP局域网传送东西,除了对传输层的TCP/UDP支持良好外,Android对HTTP(超文本传输协议)也提供了很好的支持,这里包括两种接口: 1.标准Java接口(java.net) ----HttpURLConnection,可以实现简单的基于URL请求.响应功能: 2.Apache接口(org.appache.http)----HttpClient,使用起来更方