Android 网络提交数据(使用Asynchronous Http Client)

jopen 10年前

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

<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"      tools:context=".MainActivity" >        <TextView          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:background="#88ff98ff"          android:gravity="center"          android:text="使用Async框架"          android:textSize="20sp" />        <Button          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:onClick="get_click"          android:text="GET发送HTTP请求" />        <Button          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:onClick="post_click"          android:text="POST发送HTTP请求" />        <Button          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:onClick="upload_click"          android:text="上传文件" />    </LinearLayout>

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();     }    });   }  }