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前面的改为自己的本机的服务器地址即可

你可能感兴趣的:(android)