HTTP Post连接android发送文本和图像?

时间:2014-12-23 11:49:24

标签: android http-post

我正在尝试创建HTTP POST连接以将数据和图像发送到服务器。但问题是我的URL有3个查询参数。 示例:" www.url.aspx?uname = xxx& pass = xxxx& style = xxxx"

这是我迄今为止尝试的代码

public String httppost(Bitmap cameraBitmap, String url, String data) {
      /* Create the channel for communicaton */
      InputStream is = null;
      /* Send request to server */

      try {
        /* Create the POST */
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);



        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        byte[] b = null;
        cameraBitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
        b = stream.toByteArray();
        ArrayList<NameValuePair> arguments =  new ArrayList<NameValuePair>(); 
        arguments.add(new BasicNameValuePair("data", data));
        arguments.add(new BasicNameValuePair("image", Base64.encode(b, 0, b.length)));
        /* Add the login information "POST" variables in the php */
        httppost.setEntity(new UrlEncodedFormEntity(arguments));

        /* Execute the http POST and get the response */
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
      } catch (Exception e) {
        Log.e("", "Error in http connection " + e.toString());
        return null;
      }

      /* Read response from server */
      try {
        /* Read the response stream */
        BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8);

        /* Copy the response to StringBuilder */
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
          sb.append(line + "\n");
        }
        is.close();

        /* Return the response as string */
        return sb.toString();
      } catch (Exception e) {
        Log.e("", "Error converting result " + e.toString());
        return null;
      }
    }

但它不起作用。

如何将查询字符串添加到URL?

0 个答案:

没有答案