開源專案方式提交資料-AsyncHttpClient
阿新 • • 發佈:2018-11-16
開源專案方式提交資料-AsyncHttpClient
get提交
引用android-async-http-1.4.5.jar
private static final int TIME_OUT = 5000;
protected static final int TIME_OUT2 = 3000;
private EditText et_text1;
private EditText et_text2;
private String PATH = "http://192.168.37.2:8080/http/loginServlet";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_text1 = (EditText) findViewById(R.id.et_text1);
et_text2 = (EditText) findViewById(R.id.et_text2);
}
//get提交
public void click1(View view) throws Exception{
final String username = et_text1.getText().toString().trim();
final String password = et_text2.getText().toString().trim();
if (TextUtils.isEmpty(username)&&TextUtils.isEmpty(password)) {
Toast.makeText(this, "使用者名稱和密碼不能為空" , Toast.LENGTH_SHORT).show();
return;
}
AsyncHttpClient httpClient = new AsyncHttpClient();
httpClient.get(PATH+"?username="+username+"&password="+password, new TextHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers,
String responseString) {
Toast.makeText(MainActivity.this, "get方式提交成功:"+username+"/"+password, Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(int statusCode, Header[] headers,
String responseString, Throwable throwable) {
Toast.makeText(MainActivity.this, "post方式提交失敗", Toast.LENGTH_SHORT).show();
}
});
}
post提交
//post提交
public void click2(View view){
final String username = et_text1.getText().toString().trim();
final String password = et_text2.getText().toString().trim();
if (TextUtils.isEmpty(username)&&TextUtils.isEmpty(password)) {
Toast.makeText(this, "使用者名稱和密碼不能為空", Toast.LENGTH_SHORT).show();
return;
}
AsyncHttpClient httpClient = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.add("username", username);
params.add("password", password);
httpClient.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) {
// TODO Auto-generated method stub
}
});
}
public class StreamUtils {
public static String stream2string(InputStream is) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int len=-1;
byte[] buffer = new byte[1024];
while((len=is.read(buffer))!=-1){
bos.write(buffer, 0, len);
}
is.close();
bos.close();
return bos.toString();
}
}
檔案上傳
private TextView tv_file;
private static String PATH = "http://192.168.37.2:8080/http/fileuploadServlet";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_file = (TextView) findViewById(R.id.tv_file);
}
//檔案上傳,從本地上傳到伺服器
public void click(View view) throws FileNotFoundException{
//獲取本地圖片地址/mnt/sdcard/10.jpg
String filePath = tv_file.getText().toString().trim();
AsyncHttpClient httpClient = new AsyncHttpClient();
RequestParams params = new RequestParams();
File file = new File(filePath);
params.put("file", file);
httpClient.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) {
}
});
}