0

我正在尝试使用 HTTPPOST 在 android 中上传图像和一些数据以及图像,但我收到 404 响应代码。我正在尝试使用 nameValuedPair() 发送数据。请帮我。请检查我为上传图片而编写的以下代码。

public void postImageToServer(Bitmap bitmap)
{
    ByteArrayOutputStream stream=new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);
    byte[] byte_arr=stream.toByteArray();
    String image_str=Base64.encodeToString(byte_arr, Base64.NO_WRAP|Base64.URL_SAFE);
    ArrayList<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("source","android"));
    nameValuePairs.add(new BasicNameValuePair("source_version","1.0.1"));
    nameValuePairs.add(new BasicNameValuePair("venue_name","Soda Bars"));
    nameValuePairs.add(new BasicNameValuePair("artist_name","Led Zepplin"));
    nameValuePairs.add(new BasicNameValuePair("photo", image_str));
    try{
      HttpClient httpclient = new DefaultHttpClient();
      HttpPost httppost = new HttpPost(url);
      UrlEncodedFormEntity entity=new UrlEncodedFormEntity(nameValuePairs, "UTF-8");
      httppost.setEntity(entity);
      /* ResponseHandler<String> handler = new BasicResponseHandler();
      String content = httpclient.execute(httppost, handler);*/
      HttpResponse response=httpclient.execute(httppost);
      //HttpResponse response = httpclient.execute(httppost);
       System.out.println("response  "+response.getStatusLine().getStatusCode());
      String the_string_response = convertResponseToString(response);
      System.out.println("response content "+the_string_response);
      }catch(Exception e){
              System.out.println("Error in http connection "+e.toString());
      }
}
4

0 回答 0