【问题标题】:Send upload image and text to the url (Server) in JSON Format Android以 JSON 格式发送上传图片和文本到 url (Server) Android
【发布时间】:2015-11-12 14:07:14
【问题描述】:

我遇到了麻烦...我有 1 个编辑文本和 2 个按钮,一个用于发布,一个用于上传相机图像并在 imageview 中显示图像。我的代码没有错误,但是当我单击发布按钮时,图像和文本是未在服务器上显示请检查代码我是新手,所以我不知道我在哪里丢失了代码。提前谢谢你。

这是我的 PostActivity.java :

public class PostActivity extends Activity
{public static final String TAG="url";
    private static final int CAMERA_REQUEST = 1888;

    TextView submit;
    ProgressDialog pDialog;
    InputStream is;

    EditText content;
    ImageView imageView;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.plus);

        submit = (TextView) findViewById(R.id.button7);

        content = (EditText) findViewById(R.id.editText2);
        submit.setOnClickListener(new View.OnClickListener() {

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

        imageView = (ImageView)findViewById(R.id.imageView9);
        ImageButton b = (ImageButton) findViewById(R.id.imageButton5);

        b.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent cameraIntent = new     Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
        });
    }
    public void postImageData() {
        try
        {
            Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.holder);
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost("http://blackispink.com/apis/add_post.php?title="
                    + content.getText().toString() + "&content=" + content.getText().toString() + "&cat_id=760" +"&user_id=21");
            Log.v(TAG, "postURL: " + postRequest);
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();

/* example for setting a HttpMultipartMode */
            builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
            try{
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 75, bos);
                byte[] data = bos.toByteArray();
                ByteArrayBody bab = new ByteArrayBody(data, "image.jpg");
                builder.addPart("key", bab);
                builder.addPart("key4", new StringBody(content.getText().toString(), ContentType.TEXT_PLAIN));
                builder.addPart("key1", new StringBody(content.getText().toString(), ContentType.TEXT_PLAIN));
                builder.addPart("key2", new StringBody("21", ContentType.TEXT_PLAIN));
                builder.addPart("key3", new StringBody("760", ContentType.TEXT_PLAIN));
                HttpEntity entity = builder.build();
            }
            catch(Exception e){
                //Log.v("Exception in Image", ""+e);

            }
            postRequest.setEntity((HttpEntity) builder);
            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 void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            imageView.setImageBitmap(photo);
        }
    }
    public class MainTest extends AsyncTask<String, Integer, String> {

        @Override
        protected void onPreExecute() {
            pDialog = new ProgressDialog(PostActivity.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();
        }
    }
}

【问题讨论】:

  • 请把这个e.printStackTrace();放在你的catch块中,如果有异常告诉我们..

标签: android image post text server


【解决方案1】:

对于文本,你可以使用代码行

HttpClient client = new DefaultHttpClient();
                    List<NameValuePair> formparams = new ArrayList<NameValuePair>();
                    formparams.add(new BasicNameValuePair("key1","content 1"));
                    formparams.add(new BasicNameValuePair("key2", "content 2"));
                    UrlEncodedFormEntity entity;
                    try {
                        entity = new UrlEncodedFormEntity(formparams, "UTF-8");

                        HttpPost httppost = new HttpPost("url link");
                        httppost.setEntity(entity);
                        try {
                            client.execute(httppost);
                        } catch (ClientProtocolException e) {exception handler } 
                        catch (IOException e) {exception handler }
                    } catch (UnsupportedEncodingException e) {exception handler }

【讨论】:

    【解决方案2】:

    将您的图像转换为 Base 64 并将其发送到您的服务器。

    【讨论】:

      猜你喜欢
      • 2013-08-05
      • 2021-10-09
      • 2014-12-31
      • 1970-01-01
      • 1970-01-01
      • 2018-01-13
      • 1970-01-01
      • 2018-11-10
      • 1970-01-01
      相关资源
      最近更新 更多