从Android客户端将图像发布到本地服务器

时间:2013-12-07 16:26:52

标签: android

试图确定 ::服务器运行正常,android没有将图像发送到服务器

MainActivity.java

    public void postImageData() throws IOException
    {
        Drawable myDrawable = getResources().getDrawable(R.drawable.image);
        Bitmap bitmap = ((BitmapDrawable)myDrawable).getBitmap();
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost("http://127.0.0.1/Details/");
        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        try{
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(CompressFormat.JPEG, 75, bos);
            byte[] data = bos.toByteArray();
            ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
            reqEntity.addPart("key", bab);
        }
        catch(Exception e){
            //Log.v("Exception in Image", ""+e);
            reqEntity.addPart("key", new StringBody(""));
        }
        postRequest.setEntity(reqEntity);       
        HttpResponse response = httpClient.execute(postRequest);
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        String sResponse;
        StringBuilder s = new StringBuilder();
        while ((sResponse = reader.readLine()) != null) {
            s = s.append(sResponse);
        }
    }

解决此问题的任何帮助

谢谢

1 个答案:

答案 0 :(得分:0)

“catch(例外e){             //Log.v("Exception in Image“,”“+ e);             reqEntity.addPart(“key”,new StringBody(“”));         }“? 我建议你打印stackTrace ..可能在相关的try块中发生异常..