Java AsyncHttpClient使用什么来代替多端口
我可以用什么来代替多方性?我必须为AsyncHttpClient替换HTTPClinet,这是我必须替换的代码:Java AsyncHttpClient使用什么来代替多端口,java,android,apache,Java,Android,Apache,我可以用什么来代替多方性?我必须为AsyncHttpClient替换HTTPClinet,这是我必须替换的代码: File file = new File(filePath); if (file.exists()) { throw new FileNotFoundException( "Plik o podanej nazwie nie istnieje!"); } String name = file.getName();
File file = new File(filePath);
if (file.exists()) {
throw new FileNotFoundException(
"Plik o podanej nazwie nie istnieje!");
}
String name = file.getName();
byte[] data = org.apache.commons.io.FileUtils.readFileToByteArray(file);
HttpClient httpclient = HttpClientUtil.getHttpClient(context);
HttpPost httppost = new HttpPost(Util.getServerUrl(context)
+ "/AddFile");
ByteArrayBody bab = new ByteArrayBody(data, name);
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("token", new StringBody(String.valueOf(E_Gps.TOKEN)));
reqEntity.addPart("ByteArrayBody", bab);
reqEntity.addPart("filename", new StringBody(name));
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
InputStream responseInputStream = new BufferedInputStream(response
.getEntity().getContent());
return responseInputStream;
}
下面的代码为multipart,使用它您可以上传图像和json数据
公共类多部分请求{
私人对话;
int resonseCode=0;
上下文cntx;
多方实体;
public ArrayList map=new ArrayList();
公共字符串jsonData;
私有整数计数=0;
私有字符串Url;
public void SendMultiPartRequest(上下文cntx、字符串Url、字符串jsonData、,
阵列列表(地图)
{
this.cntx=cntx;
this.Url=Url;
this.map=map;
this.jsonData=jsonData;
新建ImageUploadTask().execute(count+“”,“pk”+count+”.jpg”);
}
类ImageUploadTask扩展了AsyncTask{
字符串sResponse=null;
@凌驾
受保护的void onPreExecute(){
//TODO自动生成的方法存根
super.onPreExecute();
CustomDialog.StartProgesBarDialog(cntx);
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
试一试{
字符串url=url;
int i=Integer.parseInt(参数[0]);
HttpClient HttpClient=新的DefaultHttpClient();
HttpContext localContext=新的BasicHttpContext();
HttpPost HttpPost=新的HttpPost(url);
实体=新的多方实体();
entity.addPart(“数据”,新的StringBody(jsonData));
对于(int j=0;j
可以,但我不想使用HttpPost
public class MultipartRequest {
private ProgressDialog dialog;
int resonseCode = 0;
Context cntx;
MultipartEntity entity;
public ArrayList<String> map = new ArrayList<String>();
public String jsonData;
private int count = 0;
private String Url;
public void SendMultiPartRequest(Context cntx, String Url, String jsonData,
ArrayList<String> map)
{
this.cntx = cntx;
this.Url = Url;
this.map = map;
this.jsonData = jsonData;
new ImageUploadTask().execute(count + "", "pk" + count + ".jpg");
}
class ImageUploadTask extends AsyncTask<String, Void, String> {
String sResponse = null;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
CustomDialog.StartProgessBarDialog(cntx);
}
@Override
protected String doInBackground(String... params) {
try {
String url = Url;
int i = Integer.parseInt(params[0]);
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
entity = new MultipartEntity();
entity.addPart("data", new StringBody(jsonData));
for (int j = 0; j < map.size(); j++) {
Bitmap bitmap = decodeFile(map.get(j));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
entity.addPart("photos", new ByteArrayBody(data,
"image/jpeg", map.get(j)));
}
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
entity.writeTo(bytes);
// For adding header param in request
HashMap<String, String> headers = Constant
.getHeaderParameter(cntx);
for (Map.Entry<String, String> e : headers.entrySet()) {
httpPost.setHeader(e.getKey(), e.getValue());
}
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost,
localContext);
responseCode = response.getStatusLine().getStatusCode();
Log.w("Response ", "Status line : "
+ response.getStatusLine().toString());
sResponse = EntityUtils.toString(response.getEntity());
System.out.println("sResponse : " + sResponse);
JSONObject CheckIsErrorLess = new JSONObject(sResponse);
String error = "";
String msg = "";
if (responseCode == 200) {
// Parse your response using gson or any other library
CustomDialog.StopProgessBarDialog(cntx);
} else {
// Display warning dialog
CustomDialog.StopProgessBarDialog(cntx);
}
} catch (Exception e) {
CustomDialog.StopProgessBarDialog(cntx);
}
return sResponse;
}
@Override
protected void onPostExecute(String sResponse) {
try {
if (a == 200) {
CustomDialog.StopProgessBarDialog(cntx);
((Activity) cntx).finish();
} else {
Constant.OpenErrorDialog(cntx, a);
}
if (dialog.isShowing())
dialog.dismiss();
if (sResponse != null) {
}
} catch (Exception e) {
// Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
}
public Bitmap decodeFile(String filePath) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 1024;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, o2);
return bitmap;
}
}