NoHttp的post上传文件到服务器

方法比较简单,但是是很久以前用过的了,不知道对不对,明天测试之后定论,先上代码:

在APplication中初始化:

public class UplaodApp extends Application {


@Override
public void onCreate() {
super.onCreate();

NoHttp.init(this);
}
}


在activity中调用:

package zz.itcast.upload;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;


import com.yolanda.nohttp.Binary;
import com.yolanda.nohttp.FileBinary;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.OnResponseListener;
import com.yolanda.nohttp.OnUploadListener;
import com.yolanda.nohttp.Request;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.RequestQueue;
import com.yolanda.nohttp.Response;


import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;


public class MainActivity extends Activity {


public String uploadurl = "http://192.168.27.27:8080/server/UpLoadServlet";
RequestQueue requestQueue;
private File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1.创建请求队列。
requestQueue = NoHttp.newRequestQueue();

getFile();
}


//去assets目录下拿文件
private void getFile() {
try {
file = new File(getFilesDir().getPath(),"aaa.exe");
InputStream is = getAssets().open("aaa.exe");
FileOutputStream fos = new FileOutputStream(file);
int len =-1;
byte[] buffer = new byte[1024];
while ((len=is.read(buffer))!=-1) {
fos.write(buffer, 0, len);
}
is.close();
fos.close();

} catch (IOException e) {
e.printStackTrace();
}


}


public ProgressDialog progressDialog;


public void upload(View v){

//2.创建一个请求。长传文件的话。请求方法必须是post
Request request = NoHttp.createStringRequest(uploadurl, RequestMethod.POST);
//3.给request请求添加提交的参数。
FileBinary binary  = new FileBinary(file, "aaa.exe");
//上传文件添加监听是在FileBinary中添加的。
binary.setUploadListener(88, new OnUploadListener() {

@Override
public void onProgress(int what, int progress) {
System.out.println("onProgress"+progress);
progressDialog.setProgress(progress);
}

@Override
public void onStart(int what) {
System.out.println("onStart");
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("上传中。。。");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(100);
progressDialog.show();
}

@Override
public void onFinish(int what) {
System.out.println("onFinish");
progressDialog.dismiss();
}

@Override
public void onError(int what, Exception exception) {
System.out.println("onError");
}

@Override
public void onCancel(int what) {
System.out.println("onCancel");
}
});
request.add("file",binary);
//4.添加请求队列

requestQueue.add(99, request, responseListener);
}

OnResponseListener responseListener = new OnResponseListener() {

@Override
public void onSucceed(int what, Response response) {
// System.out.println("onSucceed");

}

@Override
public void onStart(int what) {
// System.out.println("onStart");

}

@Override
public void onFinish(int what) {
// System.out.println("onFinish");

}

@Override
public void onFailed(int what, String url, Object tag, Exception exception,
int responseCode, long networkMillis) {
// System.out.println("onFailed");

}
};


}

你可能感兴趣的:(日记)