从机器人到服务器发布的图像和字符串数据作为多部分 [英] Posting image and string data as multipart from android to server

查看:132
本文介绍了从机器人到服务器发布的图像和字符串数据作为多部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 图像部分工作正常

  • 我无法摆脱的EditText的数据能够作为发送
    另一个关键值

MainActivity.java

public class MainActivity extends Activity {

    Button submit;
    ProgressDialog pDialog;
    InputStream is;

    EditText name;
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        submit = (Button) findViewById(R.id.SUBMIT_BUTTON_ID);

        name = (EditText) findViewById(R.id.editText1);
        imageView = (ImageView) findViewById(R.id.imageView1);

        submit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new MainTest().execute();


            }
        });
    }



    /**
     * Method to post the image to the server.
     * U will have to change the url which will accept the image data.
     * @throws IOException 
     */
    public void postImageData() {


        try
        {

            Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.image); 
            String NAME = name.getText().toString();

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost("http://10.0.2.2:7002/Details/");
            MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            try{
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bitmapOrg.compress(CompressFormat.JPEG, 75, bos);
                byte[] data = bos.toByteArray();
                ByteArrayBody bab = new ByteArrayBody(data, "image.jpg");
                reqEntity.addPart("key", bab);
                reqEntity.addPart("key1", NAME);
            }
            catch(Exception e){
                //Log.v("Exception in Image", ""+e);
                reqEntity.addPart("picture", 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);
            }
        }catch(Exception e){
            e.getStackTrace();
        }


    }
    public class MainTest extends AsyncTask<String, Integer, String> {

        @Override
        protected void onPreExecute() {
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading..");
            pDialog.setIndeterminate(true);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {

            postImageData();

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub

            super.onPostExecute(result);
            // data=jobj.toString();
            pDialog.dismiss();

        }

    }

}

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="168dp"
        android:layout_weight="0.41"
        android:orientation="vertical"
        tools:context=".MainActivity" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="101dp"
            android:layout_marginTop="32dp"
            android:clickable="false"
            android:src="@drawable/image" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="195dp"
            android:layout_height="wrap_content"
            android:paddingTop="50dp"
            android:layout_gravity="center"
            android:ems="10" />

    </LinearLayout>

    <Button
        android:id="@+id/SUBMIT_BUTTON_ID"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="47dp"
        android:text="SUBMIT" />

</LinearLayout>


我收到错误行

reqEntity.addPart("key1", NAME);

The method addPart(String, ContentBody) in the type MultipartEntity is not applicable for the arguments (String, String)


我怎样才能克服这个!

推荐答案

String NAME = name.getText().toString();
reqEntity.addPart("key1", NAME);

公共无效addPart(字符串名称,ContentBody contentBody)

您有 addPart 错误的PARAMS。第二个参数应该是ContentBody

You have wrong params for addPart. The second param should be ContentBody

所以改为

 reqEntity.addPart("key1", new StringBody(NAME));

参考

<一个href=\"http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/content/ContentBody.html\" rel=\"nofollow\">http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/content/ContentBody.html

检查下面是否有帮助

http://www.androidhive.info/2011/10 / Android的决策-HTTP-请求/

这篇关于从机器人到服务器发布的图像和字符串数据作为多部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆