通过 Httppost 在 android 中将图像上传到服务器
Upload Image to server in android by Httppost
如何通过相册或相机上传图片到服务器?
我尝试了 this link 的代码,但它显示了这样的错误
Error:(329, 24) error: cannot access AbstractBody
class file for org.apache.james.mime4j.message.AbstractBody not found
如果你使用async-http作为你的http客户端click-here那么你可以使用下面的方法[如果你选择async-http你可以使用这个方法]
此方法不仅可以上传图像,还可以用于对服务器进行正常的 post 调用。
YourFragment.class[这个 class 是你调用的 http 请求]
public class YourFragment extends CommanAbstract
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.your_layout, container, false);
ButterKnife.inject(this, v);
return v;
}
private void linkCall() {
// TODO Auto-generated method stub
RequestParams params = new RequestParams();
params.put("params1", "value1");
params.put("params2", "value2");
//note that "image" tag will be the tag which accepts the file to the server
params.put("image", new File("your image path from gallery or camera"));
parse(params, 1, your link here, true);
}
@Override
public void parseresult(String response, boolean success, int value) {
// TODO Auto-generated method stub
switch (value) {
case 1:
//here you can parse the link response
break;
}
}
@Override
public void error(String response) {
// TODO Auto-generated method stub
//error will come here
}
}
CommanAbstract class[扩展此 class,其中 class 您有一个 http 请求]
public abstract class CommanAbstract extends Fragment{
private static final String Url = "your link head here";
public abstract void parseresult(String response, boolean success, int value);
public abstract void error(String response);
public void parse(RequestParams params, final int value, String link,
boolean progrss) {
// TODO Auto-generated method
AsyncHttpClient client = new AsyncHttpClient();
GlobalFunctions.postApiCall(getActivity(), link, params, client,
new HttpResponseHandler() {
@Override
public void handle(String response, boolean success) {
// TODO Auto-generated method stub
if (success) {
parseresult(response, true, value);
} else {
error(response);
toast("Connection error");
}
}
});
}
}
GlobalFunctions class[此 class 调用 http 请求]
public class GlobalFunctions {
// static ProgressDialog progress;
public interface HttpResponseHandler {
void handle(String response,boolean failre);
}
public static void postApiCall(final Context context, final String url,
RequestParams params, AsyncHttpClient httpClient,
final HttpResponseHandler handler) {
httpClient.post(url, params, new AsyncHttpResponseHandler() {
@Override
public void onFailure(Throwable arg0, String failureResponse) {
// TODO Auto-generated method stub
super.onFailure(arg0, failureResponse);
System.out.println("fail" + failureResponse + "url is" + url);
handler.handle(failureResponse,false);
// errorToast(context);
}
@Override
public void onSuccess(String response) {
handler.handle(response,true);
}
});
}
}
您可以使用 http post 多部分将图像上传到服务器。这是 link 可以帮助您的。
另一种上传图片的方法是,将其转换为 Base64,然后再上传。检查下面的 link.
Android upload image to server using base64
如何通过相册或相机上传图片到服务器?
我尝试了 this link 的代码,但它显示了这样的错误
Error:(329, 24) error: cannot access AbstractBody class file for org.apache.james.mime4j.message.AbstractBody not found
如果你使用async-http作为你的http客户端click-here那么你可以使用下面的方法[如果你选择async-http你可以使用这个方法] 此方法不仅可以上传图像,还可以用于对服务器进行正常的 post 调用。
YourFragment.class[这个 class 是你调用的 http 请求]
public class YourFragment extends CommanAbstract
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v = inflater.inflate(R.layout.your_layout, container, false);
ButterKnife.inject(this, v);
return v;
}
private void linkCall() {
// TODO Auto-generated method stub
RequestParams params = new RequestParams();
params.put("params1", "value1");
params.put("params2", "value2");
//note that "image" tag will be the tag which accepts the file to the server
params.put("image", new File("your image path from gallery or camera"));
parse(params, 1, your link here, true);
}
@Override
public void parseresult(String response, boolean success, int value) {
// TODO Auto-generated method stub
switch (value) {
case 1:
//here you can parse the link response
break;
}
}
@Override
public void error(String response) {
// TODO Auto-generated method stub
//error will come here
}
}
CommanAbstract class[扩展此 class,其中 class 您有一个 http 请求]
public abstract class CommanAbstract extends Fragment{
private static final String Url = "your link head here";
public abstract void parseresult(String response, boolean success, int value);
public abstract void error(String response);
public void parse(RequestParams params, final int value, String link,
boolean progrss) {
// TODO Auto-generated method
AsyncHttpClient client = new AsyncHttpClient();
GlobalFunctions.postApiCall(getActivity(), link, params, client,
new HttpResponseHandler() {
@Override
public void handle(String response, boolean success) {
// TODO Auto-generated method stub
if (success) {
parseresult(response, true, value);
} else {
error(response);
toast("Connection error");
}
}
});
}
}
GlobalFunctions class[此 class 调用 http 请求]
public class GlobalFunctions {
// static ProgressDialog progress;
public interface HttpResponseHandler {
void handle(String response,boolean failre);
}
public static void postApiCall(final Context context, final String url,
RequestParams params, AsyncHttpClient httpClient,
final HttpResponseHandler handler) {
httpClient.post(url, params, new AsyncHttpResponseHandler() {
@Override
public void onFailure(Throwable arg0, String failureResponse) {
// TODO Auto-generated method stub
super.onFailure(arg0, failureResponse);
System.out.println("fail" + failureResponse + "url is" + url);
handler.handle(failureResponse,false);
// errorToast(context);
}
@Override
public void onSuccess(String response) {
handler.handle(response,true);
}
});
}
}
您可以使用 http post 多部分将图像上传到服务器。这是 link 可以帮助您的。
另一种上传图片的方法是,将其转换为 Base64,然后再上传。检查下面的 link.
Android upload image to server using base64