我试图让应用程序拍照,然后将照片上传到java中的服务器,但我无法摆脱这个错误
public void makeHTTPCall() {
prgDialog.setMessage("Invoking JSP");
AsyncHttpClient client = new AsyncHttpClient();
这是错误:
" Class'从asynchttpresponsehandler派生的匿名类必须 要么被宣布为抽象,要么实现抽象方法 onFailure(int,Header [],byte [],Throwable)in AsyncHttpResponseHandler"
client.post("http://192.168.2.5:9999/ImageUploadWebApp/uploadimg.jsp", params, new AsyncHttpResponseHandler() {
在以下两种方法中,它还表示他们不会超越超类
@Override
public void onSuccess(String response) {
// Hide Progress Dialog
prgDialog.hide();
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
}
@Override
public void onFailure(int statusCode, Throwable error, String content) {
// Hide Progress Dialog
prgDialog.hide();
// When Http response code is '404'
if (statusCode == 404) {
Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
}
// When Http response code is '500'
else if (statusCode == 500) {
Toast.makeText(getApplicationContext(),
"Something went wrong at server end",
Toast.LENGTH_LONG).show();
}
// When Http response code other than 404, 500
else {
Toast.makeText(
getApplicationContext(),
"Error Occured \n Most Common Error: \n1. Device not connected to Internet\n2. Web App is not deployed in App server\n3. App server is not running\n HTTP Status code : "
+ statusCode, Toast.LENGTH_LONG)
.show();
}
}
});
}
我遵循本教程Tutorial
答案 0 :(得分:1)
只需使用以下方法通过json请求发送多个文件...
mImagePath是图像路径的arraylist
// Method for sending files using multiparting......
public static String sendJsonWithFile(Activity mActivity, ArrayList<String> mImagePaths, String jsonString, String URL)
{
Log.e("json", jsonString);
String res = "";
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.2.5:9999/ImageUploadWebApp/uploadimg.jsp");
String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****";
boundary = "--" + boundary;
httppost.addHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
StringBody stringBody = new StringBody(jsonString);
reqEntity.addPart("formstring", stringBody);
for (int i = 0; i < mImagePaths.size(); i++)
{
String imagePath = mImagePaths.get(i);
if (mImagePaths != null && mImagePaths.size() > 0)
{
byte[] filebytes = FileUtils.readFileToByteArray(new File(imagePath));
ByteArrayBody filebodyImage = new ByteArrayBody(filebytes, "image");
Log.e("file path=", filebodyImage.toString());
reqEntity.addPart("image", filebodyImage);
}
}
httppost.setEntity(reqEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null)
{
res = EntityUtils.toString(resEntity);
System.out.println(res);
}
if (resEntity != null)
{
resEntity.consumeContent();
}
httpclient.getConnectionManager().shutdown();
}
catch (UnsupportedEncodingException e)
{
res = "UnsupportedEncodingException";
e.printStackTrace();
}
catch (ClientProtocolException e)
{
res = "ClientProtocolException";
e.printStackTrace();
}
catch (FileNotFoundException e)
{
res = "FileNotFoundException";
e.printStackTrace();
}
catch (IOException e)
{
res = "IOException";
e.printStackTrace();
}
catch (Exception e)
{
res = "Exception";
e.printStackTrace();
}
return res;
}