发布串放;图像从Android本地服务器 [英] Posting string & image to local server from android

查看:121
本文介绍了发布串放;图像从Android本地服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我想不出解决我的问题,我想出了一个不同的方法

Since i could not figure solve my problem, i had come up with a different approach

现在我已经用了两个职位的功能

now i have used two post functions


  • 一个多部分图像发布------------> postImageData()

  • 另一个字符串数据发布------------> POSTDATA()

  • one for multipart image posting ------------ >postImageData()
  • another for string data posting ------------ > postData()

我想发布一个单一的形象作为一个多,没有多部分字符串数据

I am trying to post a single image as a multipart and a string data without multipart

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();


            }
        });
    }

    public void postData() {

        String newurl = "?" + "key1=" + name.getText().toString();
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://10.0.2.2:7002/Details/"+newurl);

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("key1", name.getText()
                    .toString()));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            Log.v("Response", response.toString());

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }


    /**
     * 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); 

            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", new StringBody(name.getText().toString()));
            }
            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();
            postData();

            return null;
        }

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

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

        }

    }

}


的问题,我现在面临


  • 目前的所有操作postImageData()工作正常

  • POSTDATA()目标不解决

  • 服务器端我收到错误无法读取属性关键的
    未定义

  • Currently all the operations of postImageData() are working fine
  • but postData() objective is not solved
  • Server side i am getting error Cannot read property 'key' of undefined

注意 ::我检查服务器是否正常工作时的数据与邮差送

Note :: i checked that server is correctly working when data is sent with POSTMAN

测试,我发现成功的邮递员

推荐答案

有关,你必须通过它在服务器端的关键是:

For that you have to pass the key which is on server side:

我爱:
 如果你的服务器端已定义为一个图片,然后在客户端时,你必须张贴数据到服务器,你必须传递键名作为参数的关键。

Like: if your server side you have defined your key as a "picture" then on client side when you have to post that data to server you have to pass that key name as a parameter.

我爱:

  reqEntity.addPart("picture", bab);

这篇关于发布串放;图像从Android本地服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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