Http Post请求在Android应用中进行测试

时间:2014-10-30 14:15:47

标签: java android http httpresponse multipartentity

我正在写一个小应用程序,当从相机拍摄照片时上传照片。以下是完整的代码。当我在手机上安装它并运行应用程序时,我看不到apache tomcat日志中的任何内容(即发布请求无法执行)。

我在手机上使用网络浏览器对后端进行了测试,但它确实有效。但是,当我尝试使用应用程序时,我不会在手机和服务器日志中看到任何输出。

有人能告诉我代码中是否有任何问题?如果不是如何处理这些类型的错误。

已编辑的代码

public class MainActivity extends Activity {

    int REQUEST_CODE = 1;
    ImageView IMG;
    Bitmap bitmap;
    TextView link;
    Bitmap bm[] = new Bitmap [4];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (i.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(i, REQUEST_CODE);
        }
        setContentView(R.layout.activity_main);
        IMG = (ImageView) findViewById(R.id.img);
        link = (TextView) findViewById(R.id.response);
    }

    public void onActivityResult(int requestcode, int resultcode, Intent data) {
        if (requestcode == REQUEST_CODE) {
            if (resultcode == RESULT_OK) {
                Bundle bundel = new Bundle();
                bundel = data.getExtras();
                bitmap = (Bitmap) bundel.get("data");
                bm[0] = bitmap;
                UploadPic picUpload = new UploadPic();
                picUpload.execute(bm);
                IMG.setImageBitmap(bitmap);

            }

        }

    }

    private class UploadPic extends AsyncTask<Bitmap, Integer, String> {

        @Override
        protected String doInBackground(Bitmap... bitmap) {
            // TODO Auto-generated method stub
            BufferedReader in = null;
            String data = null;

            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://192.168.1.185:8080/ver0_2/query");
                MultipartEntityBuilder builder = MultipartEntityBuilder.create();

                /* example for setting a HttpMultipartMode */
                builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                bitmap[0].compress(CompressFormat.JPEG, 75, byteArrayOutputStream);
                // bitmap.compress
                byte [] byteData = byteArrayOutputStream.toByteArray();
                // String strData = Base64.encodeToString(data, Base64.DEFAULT); // I have no idea why Im doing this
                ByteArrayBody byteArrayBody = new ByteArrayBody(byteData, "image"); // second parameter is the name of the image

                /* example for adding an image part */
                builder.addPart("fileUpload", byteArrayBody);
                HttpEntity entity = builder.build();
                httppost.setEntity(entity);
                HttpResponse response = httpclient.execute(httppost);
                in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                StringBuffer sb = new StringBuffer("");
                String l = "";
                String nl = System.getProperty("line.seperator");
                while ((l = in.readLine()) != null) {
                    sb.append(l + nl);
                }

                in.close();
                data = sb.toString();

            }
            catch (Exception e) {
                e.printStackTrace();
            }
            return data;
        }

        @Override
        protected void onPostExecute(String result) {
            // execution of result of Long time consuming operation
            link.setText(result);
        }
    }
}

0 个答案:

没有答案