1

我正在开发小型 android 应用程序,在该应用程序中,我想使用 put 方法将图像上传到我的服务器,并将图像作为带有 json 对象的请求体。我对此提出了很多问题,这对我有很大帮助。我尝试了以下方式...

class ImageUploadTask extends AsyncTask <Void, Void, Void>{
    @Override
      protected Void doInBackground(Void... unused) {

         HttpClient hc = new DefaultHttpClient();
            String message;
            HttpPut p = new HttpPut("https://abc.com");
            JSONObject Jobject = new JSONObject();

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(CompressFormat.JPEG, 100, bos);

            byte[] data = bos.toByteArray();

            Jobject.put("Image", data);


        try {
        message = Jobject.toString();

        p.setEntity(new StringEntity(message, "UTF8"));
        p.setHeader("Content-type", "application/json");
            HttpResponse resp = hc.execute(p);

            if (resp != null) {
                if (resp.getStatusLine().getStatusCode() == 204)
                {

                }
            }

            Log.d("Status line", "" + resp.getStatusLine().getStatusCode());
        } catch (Exception e) {
            e.printStackTrace();

        }
        return null;


    }

    @Override
    protected void onProgressUpdate(Void... unsued) {

    }

    @Override
    protected void onPostExecute(Void result) {
        try {
            if (dialog.isShowing())
                dialog.dismiss();

        } catch (Exception e) {

            Toast.makeText(getApplicationContext(),"Error"+e,
                    Toast.LENGTH_LONG).show();
            Log.e(e.getClass().getName(), e.getMessage(), e);
        }
    }
}

但它不工作。当我尝试执行我的网络调用时,它向我显示系统错误 java.net.UnknownHostException: abc.com。我做错了什么。有没有办法做到这一点。需要帮忙。谢谢你。

4

1 回答 1

0

我试图做同样的事情也许我们可以帮助我们,这是我的代码:

private class UploadFileToServer extends AsyncTask<Void, Void, Void> {

    Bitmap image;
    String name;

    public UploadFileToServer(Bitmap img, String name){
        this.image = img;
        this.name = name;

    }
    @Override
    protected Void doInBackground(Void... params) {
        //creation of bytearray
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        guardem al byteArrayOutputStream anterior
        this.image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
        //codifing image
        String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT);
        //array list creation and add what we want to send
        ArrayList<NameValuePair> dataToSend = new ArrayList<>();
        dataToSend.add(new BasicNameValuePair("image",encodedImage));
        dataToSend.add(new BasicNameValuePair("name",this.name));
        //i have dont understand so much from here to down....
        HttpParams HttpRequestParams = getHttpRequestParams();

        //send data by post server adres is my local api "ip/myapi/index.php"
        HttpClient client = new DefaultHttpClient(HttpRequestParams);
        HttpPost post = new HttpPost(SERVER_ADRESS+"index.php");

        try{
            post.setEntity(new UrlEncodedFormEntity(dataToSend));
//i take this part from you i hope it will work not tryied yet
            HttpResponse response = client.execute(post);
            if (response != null) {
                if (response.getStatusLine().getStatusCode() == 204)
                {
                      //so when you are here its all right?
                }
            }
        } catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    private HttpParams getHttpRequestParams(){
        HttpParams HttpRequestParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(HttpRequestParams, 1000 * 30);
        HttpConnectionParams.setSoTimeout(HttpRequestParams,1000*30);
        return HttpRequestParams;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        Toast.makeText(getApplicationContext(), "image uploaded", Toast.LENGTH_LONG).show();
    }
}
于 2016-03-16T15:42:17.367 回答