【问题标题】:Error trying to upload an image from android to a server尝试将图像从 android 上传到服务器时出错
【发布时间】:2015-09-06 21:19:22
【问题描述】:

我正在尝试制作一个应用程序来拍照,然后将照片上传到 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

【问题讨论】:

    标签: android http web server


    【解决方案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;
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-11
      • 2019-11-03
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 2014-08-05
      • 1970-01-01
      相关资源
      最近更新 更多