zoukankan      html  css  js  c++  java
  • Android 网络提交数据(使用Asynchronous Http Client)

    项目主页及简单使用方法http://loopj.com/android-async-http/

    页面布局就不复制了,把主要的Activity记录下来,供自己以后使用:

    package com.example.asynchttp;
     
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.URI;
    import java.net.URLEncoder;
     
    import org.apache.http.Header;
    import org.apache.http.HttpResponse;
     
    import com.loopj.android.http.AsyncHttpClient;
    import com.loopj.android.http.AsyncHttpResponseHandler;
    import com.loopj.android.http.RequestParams;
    import com.loopj.android.http.ResponseHandlerInterface;
    import com.loopj.android.http.TextHttpResponseHandler;
     
    import android.os.Bundle;
    import android.provider.MediaStore.Files;
    import android.app.Activity;
    import android.content.res.Resources;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Toast;
     
    public class MainActivity extends Activity
    {
     
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
     
        @Override
        public boolean onCreateOptionsMenu(Menu menu)
        {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
     
        public void get_click(View v) throws Exception
        {
            String path = "http://192.168.1.100:8080/ServletTest/Login" + "?username=" + URLEncoder.encode("test", "utf-8") + "&password=" + URLEncoder.encode("123", "utf-8");
            AsyncHttpClient client = new AsyncHttpClient();
            client.get(path, new AsyncHttpResponseHandler()
            {
     
                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody)
                {
                    Toast.makeText(MainActivity.this, new String(responseBody), Toast.LENGTH_SHORT).show();
                }
     
                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error)
                {
                    Toast.makeText(MainActivity.this, new String(responseBody + error.getMessage()), Toast.LENGTH_SHORT).show();
                }
            });
        }
     
        public void post_click(View v) throws Exception
        {
            String path = "http://192.168.1.100:8080/ServletTest/Login";
            String username = "test";
            String password = "123";
            AsyncHttpClient client = new AsyncHttpClient();
            RequestParams params = new RequestParams();
            params.add("username", username);
            params.add("password", password);
            client.post(path, params, new TextHttpResponseHandler()
            {
                @Override
                public void onSuccess(int statusCode, Header[] headers, String responseString)
                {
                    Toast.makeText(MainActivity.this, responseString, Toast.LENGTH_SHORT).show();
                }
     
                @Override
                public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable)
                {
                    Toast.makeText(MainActivity.this, responseString, Toast.LENGTH_SHORT).show();
                }
            });
        }
     
        public void upload_click(View v) throws Exception
        {
            String path = "http://192.168.1.100:8080/ServletTest/Upload";
            AsyncHttpClient client = new AsyncHttpClient();
            RequestParams params = new RequestParams();
            InputStream is = getAssets().open("test.png");
            params.put("file", is,"测试.png");
            client.post(path, params, new AsyncHttpResponseHandler()
            {
     
                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody)
                {
                    Toast.makeText(MainActivity.this, "上传成功", Toast.LENGTH_SHORT).show();
                }
     
                @Override
                public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error)
                {
                    Toast.makeText(MainActivity.this, "上传失败", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
    调试测试的时候可以使用tomvat服务器试验下,开始自己不会写服务器端的代码,偶然间发现toncat自带的例子里面有测试post和get方法提交数据的,一般的地址为:http://10.3.19.27:8080/examples/servlets/servlet/SessionExample前面的改为自己的本机的服务器地址即可

  • 相关阅读:
    java 事件监听机制组成
    关于父进程和子进程的关系(UAC 绕过思路)
    Fort.js – 时尚、现代的进度提示效果
    Hive学习之函数DDL和Show、Describe语句
    js完美的div拖拽实例代码
    SSH2框架实现注冊发短信验证码实例
    再看C#中的托付和事件
    RGB(FFFFFF)转255:255:255
    单一目的聚集操作
    智慧城市,在中国的北海边再画一个圈——大连“中国首届智慧城市协同创新峰会”请你带好笔
  • 原文地址:https://www.cnblogs.com/sowhat4999/p/4439860.html
Copyright © 2011-2022 走看看