Android 網絡提交數據(使用Asynchronous Http Client)
項目主頁及簡單使用方法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();
}
});
}
} 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!