【问题标题】:The method setRequestProperty(String, String) in the type URLConnection is not applicable for the arguments (String, ArrayList<String>)URLConnection 类型中的方法 setRequestProperty(String, String) 不适用于参数 (String, ArrayList<String>)
【发布时间】:2015-04-20 05:33:10
【问题描述】:

我正在尝试将多个图像发送到服务器但是,所以我将所有图像存储在一个数组列表中,但是之后当我需要发送到服务器时,它在行附近显示错误,,....... ......

 conn.setRequestProperty("image", multimgss); 

错误

The method setRequestProperty(String, String) in the type URLConnection is not applicable for the arguments (String, ArrayList<String>)..

主活动

    public int uploadFile(String sourceFileUri) {


      String fileName = sourceFileUri;

      /*String[] mimgs = null;
      mimgs.equals(fileName);*/

      ArrayList<String> multimgss=new ArrayList<String>();
      multimgss.add(fileName);

      System.out.println("multipleimagesss"+multimgss);


    try {
        abcd = new JSONArray(multimgss.toString());
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
      //abcd=multimgss.add(fileName);


      HttpURLConnection conn = null;
      DataOutputStream dos = null;  
      String lineEnd = "\r\n";
      String twoHyphens = "--";
      String boundary = "*****";
      int bytesRead, bytesAvailable, bufferSize;
      byte[] buffer;
      int maxBufferSize = 1 * 1024 * 1024; 
      File sourceFile = new File(sourceFileUri); 

      if (!sourceFile.isFile()) {

           dialog.dismiss(); 

           Log.e("uploadFile", "Source File not exist :"+imagepath);

           runOnUiThread(new Runnable() {
               public void run() {
                   messageText.setText("Source File not exist :"+ imagepath);
               }
           }); 

           return 0;

      }
      else
      {
           try { 


               String cats[]=acTextView.getText().toString().split(",");
               JsonParse jp=new JsonParse();
               for(String cat:cats){
                   List<SuggestGetSet> list =jp.getParseJsonWCF(acTextView.getText().toString());
                for(int i = 0;i<list.size();i++){
                  if(list.get(i).getName().equals(cat))
                      catid.add(list.get(i).getId().toString());
                   //params.add(new BasicNameValuePair("parentid",list.get(i).getId()));


                } 
               }



               catsids=catid.toString();
               pname = proname.getText().toString();
                pskucode = proskucode.getText().toString();
                psalerate=prosalerate.getText().toString();
                ppurchaserate=propurchaserate.getText().toString();
                pweight = proweight.getText().toString();
                pweighttype=proweighttype.getText().toString();
            //  pproactives = proactives.getTag().toString();

                 System.out.println("active nu stauts"+catsids);
            //  pdisporder = prodisporder.getText().toString();
                pdesc = prodesc.getText().toString();

                upLoadServerUri = "http://webistname/web-service/addproduct.php?action=addproduct&name="+pname+"&categoryid="+catsids+"&skucode="+pskucode+"&sale_rate="+psalerate+"&purchase_rate="+ppurchaserate+"&weight="+pweight+"&weighttype="+pweighttype+"&description=hello&image="+abcd;




                 // open a URL connection to the Servlet
               FileInputStream fileInputStream = new FileInputStream(sourceFile);
               URL url = new URL(upLoadServerUri);

               // Open a HTTP  connection to  the URL
               conn = (HttpURLConnection) url.openConnection(); 
               conn.setDoInput(true); // Allow Inputs
               conn.setDoOutput(true); // Allow Outputs
               conn.setUseCaches(false); // Don't use a Cached Copy
               conn.setRequestMethod("POST");
               conn.setRequestProperty("Connection", "Keep-Alive");
               conn.setRequestProperty("ENCTYPE", "multipart/form-data");
               conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

               conn.setRequestProperty("name", pname);
               conn.setRequestProperty("categoryid", catsids);
               conn.setRequestProperty("skucode", pskucode);
               conn.setRequestProperty("sale_rate", psalerate);
               conn.setRequestProperty("purchase_rate", ppurchaserate);
               conn.setRequestProperty("weight", pweight);
               conn.setRequestProperty("weighttype", pweighttype);
               conn.setRequestProperty("description", "hello");
             //  conn.setRequestProperty("displayorder", pdisporder);
              // conn.setRequestProperty("active", pproactives);
               conn.setRequestProperty("image", abcd.toString()); 


               dos = new DataOutputStream(conn.getOutputStream());

               dos.writeBytes(twoHyphens + boundary + lineEnd); 

               dos.writeBytes("Content-Disposition: form-data; name=\"type\""
                         + lineEnd);
                 dos.writeBytes(lineEnd);

                 // assign value
                 /*dos.writeBytes("version=apps");
                 dos.writeBytes(lineEnd);
                 dos.writeBytes(twoHyphens + boundary + lineEnd);*/

                 dos.writeBytes("name="+pname);
                 dos.writeBytes(lineEnd);
                 dos.writeBytes(twoHyphens + boundary + lineEnd);


                 dos.writeBytes("categoryid="+catsids);
                 dos.writeBytes(lineEnd);
                 dos.writeBytes(twoHyphens + boundary + lineEnd);

                 dos.writeBytes("skucode="+pskucode);
                 dos.writeBytes(lineEnd);
                 dos.writeBytes(twoHyphens + boundary + lineEnd);

                 dos.writeBytes("sale_rate="+psalerate);
                 dos.writeBytes(lineEnd);
                 dos.writeBytes(twoHyphens + boundary + lineEnd);

                 dos.writeBytes("purchase_rate="+ppurchaserate);
                 dos.writeBytes(lineEnd);
                 dos.writeBytes(twoHyphens + boundary + lineEnd);

                 dos.writeBytes("weight="+pweight);
                 dos.writeBytes(lineEnd);
                 dos.writeBytes(twoHyphens + boundary + lineEnd);

                 dos.writeBytes("weighttype="+pweighttype);
                 dos.writeBytes(lineEnd);
                 dos.writeBytes(twoHyphens + boundary + lineEnd);

                 dos.writeBytes("description="+"hello");
                 dos.writeBytes(lineEnd);
                 dos.writeBytes(twoHyphens + boundary + lineEnd);


                /* dos.writeBytes("displayorder="+pdisporder);
                 dos.writeBytes(lineEnd);
                 dos.writeBytes(twoHyphens + boundary + lineEnd);

                 dos.writeBytes("active="+pproactives);
                 dos.writeBytes(lineEnd);
                 dos.writeBytes(twoHyphens + boundary + lineEnd);*/



               dos.writeBytes("Content-Disposition: form-data; name=\"image\";filename=\""
                                         + abcd.toString() + "\"" + lineEnd);

               dos.writeBytes(lineEnd);

               // create a buffer of  maximum size
               bytesAvailable = fileInputStream.available(); 

               bufferSize = Math.min(bytesAvailable, maxBufferSize);
               buffer = new byte[bufferSize];

               // read file and write it into form...
               bytesRead = fileInputStream.read(buffer, 0, bufferSize);  

               while (bytesRead > 0) {

                 dos.write(buffer, 0, bufferSize);
                 bytesAvailable = fileInputStream.available();
                 bufferSize = Math.min(bytesAvailable, maxBufferSize);
                 bytesRead = fileInputStream.read(buffer, 0, bufferSize);   

                }

               // send multipart form data necesssary after file data...
               dos.writeBytes(lineEnd);
               dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

               // Responses from the server (code and message)
               serverResponseCode = conn.getResponseCode();
               String serverResponseMessage = conn.getResponseMessage();

               Log.i("uploadFile", "HTTP Response is : "
                       + serverResponseMessage + ": " + serverResponseCode);

               if(serverResponseCode == 200){

                   runOnUiThread(new Runnable() {
                        public void run() {
                            String msg = "File Upload Completed.\n\n See uploaded file here : \n\n"
                                  +" F:/wamp/wamp/www/uploads";
                            messageText.setText(msg);
                            Toast.makeText(MainActivity.this, "File Upload Complete.", Toast.LENGTH_SHORT).show();
                        }
                    });                
               }    

完整代码

公共类 MainActivity 扩展 Activity 实现 OnClickListener {

private static int RESULT_LOAD_IMAGE = 1;
private TextView messageText;
private Button uploadButton;
private ImageView imageview;
private int serverResponseCode = 0;
private ProgressDialog dialog = null;

private String upLoadServerUri = null;
private String imagepath=null;

private EditText proname;
private EditText proskucode;

private EditText prodesc;
//private CheckBox proactives;
private ImageView btnselectpic;

private String pskucode;


private EditText prosalerate;
private EditText propurchaserate;
private EditText proweight;
private EditText proweighttype;
//private EditText prodisporder;
private String pname;
private String psalerate;
private String ppurchaserate;
private String pweight;
private String pweighttype;
private String pproactives;
private String pdisporder;
private String pdesc;
private List<String> catid=new ArrayList<String>();
private AutoCompleteTextView acTextView;
private String catsids;
private Uri selectedImageUri;
private int columnIndex;
private ImageAdapter objImageAdapter;
private GridView gridView;
private JSONArray abcd;
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    gridView = (GridView) findViewById(R.id.gridview);
    //gridView.setAdapter(new ImageAdapter(this));

    objImageAdapter= new ImageAdapter(this); 
    gridView.setAdapter(objImageAdapter);

    gridView.setOnItemClickListener(new OnItemClickListener() 
    {
        public void onItemClick(AdapterView<?> parent, 
        View v, int position, long id) 
        {                
            Toast.makeText(getBaseContext(), 
                    "pic" + (position + 1) + " selected", 
                    Toast.LENGTH_SHORT).show();
        }
    });        

    acTextView = (AutoCompleteTextView) findViewById(R.id.multautoComplete);
    acTextView.setAdapter(new SuggestionAdapter(this,acTextView.getText().toString()));
  //  acTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

   proname=(EditText)findViewById(R.id.edt_proname);
    proskucode=(EditText)findViewById(R.id.edt_proskucode);
    prosalerate=(EditText)findViewById(R.id.edt_salerate);
    propurchaserate=(EditText)findViewById(R.id.edt_purchaserate);
    proweight=(EditText)findViewById(R.id.edt_weight);
    proweighttype=(EditText)findViewById(R.id.edt_weighttype);
 //  prodesc=(EditText)findViewById(R.id.edt_desc);
  // prodisporder=(EditText)findViewById(R.id.edt_disporder);
  // proactives=(CheckBox)findViewById(R.id.edt_proactive);

   uploadButton = (Button)findViewById(R.id.btn_addpro);
    messageText  = (TextView)findViewById(R.id.edt_imgs);
    btnselectpic = (ImageView)findViewById(R.id.browsesimgs);
    imageview = (ImageView)findViewById(R.id.imagevw);


    btnselectpic.setOnClickListener(this);
    uploadButton.setOnClickListener(this);

}


@Override
public void onClick(View arg0) {
    if(arg0==btnselectpic)
    {
        /*Intent intent=new Intent(MainActivity.this,PhotoSelection.class);
        startActivity(intent);*/

        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(intent, 1);
        /*Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Complete action using"), 1);*/

        /*Intent i = new Intent(Action.ACTION_MULTIPLE_PICK);
        startActivityForResult(i, 200);*/
    }
    else if (arg0==uploadButton) {

         dialog = ProgressDialog.show(MainActivity.this, "", "Uploading file...", true);
         messageText.setText("uploading started.....");
         new Thread(new Runnable() {
             public void run() {            
                  uploadFile(imagepath);                       
             }
           }).start();     
    }

} 

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

     if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
         selectedImageUri= data.getData();
         String[] filePathColumn = { MediaStore.Images.Media.DATA };

         Cursor cursor = getContentResolver().query(selectedImageUri,
                 filePathColumn, null, null, null);
         cursor.moveToFirst();

        /* columnIndex = cursor.getColumnIndex(filePathColumn[0]);
         picturePath = cursor.getString(columnIndex);
         cursor.close();*/

       //  imageView = (ImageView) findViewById(R.id.imgView);
         imageview.setImageBitmap(BitmapFactory.decodeFile(imagepath));

         columnIndex = cursor.getColumnIndex(filePathColumn[0]);
         String picturePath = cursor.getString(columnIndex);
         objImageAdapter.addToList(picturePath);
         cursor.close();
        // addmores.setVisibility(View.VISIBLE);
     }

    /*if (requestCode == 1 && resultCode == RESULT_OK) {
        //Bitmap photo = (Bitmap) data.getData().getPath(); 

        selectedImageUri = data.getData();
        imagepath = getPath(selectedImageUri);
        Bitmap bitmap=BitmapFactory.decodeFile(imagepath);
        imageview.setImageBitmap(bitmap);
        messageText.setText("Uploading file path:" +imagepath);

    }*/
}
     public String getPath(Uri uri) {
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = managedQuery(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }

     public class ImageAdapter extends BaseAdapter 
     {
   private Context context;
   ArrayList<String> arrayList=new ArrayList<String>();

   public ImageAdapter(Context c) 
   {
       context = c;
   }

   //---returns the number of images---
   public int getCount() {
       return arrayList.size();
   }

   //---returns the ID of an item--- 
   public Object getItem(int position) {
       return position;
   }

   void addToList(String strPath)
   {
       this.arrayList.add(strPath);
       this.notifyDataSetChanged();
   }
   public long getItemId(int position) {
       return position;
   }

   //In this array you have to store all images path which is you want to display in baseapater and must be global to access in baseapater  

   public View getView(int position, View convertView, ViewGroup parent) 
   {
       ImageView imageView;
       if (convertView == null) {
           imageView = new ImageView(context);
           imageView.setLayoutParams(new GridView.LayoutParams(185, 185));
           imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
           imageView.setPadding(5, 5, 5, 5);
       } else {
           imageView = (ImageView) convertView;
       }
       String path = arrayList.get(position);
       Bitmap myBitmap = BitmapFactory.decodeFile(path);
       imageView.setImageBitmap(myBitmap);
       return imageView;
   }
    }    

public int uploadFile(String sourceFileUri) {


      String fileName = sourceFileUri;

      /*String[] mimgs = null;
      mimgs.equals(fileName);*/

      ArrayList<String> multimgss=new ArrayList<String>();
      multimgss.add(fileName);

      System.out.println("multipleimagesss"+multimgss);


    try {
        abcd = new JSONArray(multimgss.toString());
    } catch (JSONException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
      //abcd=multimgss.add(fileName);


      HttpURLConnection conn = null;
      DataOutputStream dos = null;  
      String lineEnd = "\r\n";
      String twoHyphens = "--";
      String boundary = "*****";
      int bytesRead, bytesAvailable, bufferSize;
      byte[] buffer;
      int maxBufferSize = 1 * 1024 * 1024; 
      File sourceFile = new File(sourceFileUri); 

      if (!sourceFile.isFile()) {

           dialog.dismiss(); 

           Log.e("uploadFile", "Source File not exist :"+imagepath);

           runOnUiThread(new Runnable() {
               public void run() {
                   messageText.setText("Source File not exist :"+ imagepath);
               }
           }); 

           return 0;

      }
      else
      {
           try { 


               String cats[]=acTextView.getText().toString().split(",");
               JsonParse jp=new JsonParse();
               for(String cat:cats){
                   List<SuggestGetSet> list =jp.getParseJsonWCF(acTextView.getText().toString());
                for(int i = 0;i<list.size();i++){
                  if(list.get(i).getName().equals(cat))
                      catid.add(list.get(i).getId().toString());
                   //params.add(new BasicNameValuePair("parentid",list.get(i).getId()));


                } 
               }
               catsids=catid.toString();
               pname = proname.getText().toString();
                pskucode = proskucode.getText().toString();
                psalerate=prosalerate.getText().toString();
                ppurchaserate=propurchaserate.getText().toString();
                pweight = proweight.getText().toString();
                pweighttype=proweighttype.getText().toString();
            //  pproactives = proactives.getTag().toString();

                 System.out.println("active nu stauts"+catsids);
            //  pdisporder = prodisporder.getText().toString();
                pdesc = prodesc.getText().toString();

                upLoadServerUri = "http://mywebsitename/web-service/addproduct.php?action=addproduct&name="+pname+"&categoryid="+catsids+"&skucode="+pskucode+"&sale_rate="+psalerate+"&purchase_rate="+ppurchaserate+"&weight="+pweight+"&weighttype="+pweighttype+"&description=hello&image="+abcd;




                 // open a URL connection to the Servlet
               FileInputStream fileInputStream = new FileInputStream(sourceFile);
               URL url = new URL(upLoadServerUri);

               // Open a HTTP  connection to  the URL
               conn = (HttpURLConnection) url.openConnection(); 
               conn.setDoInput(true); // Allow Inputs
               conn.setDoOutput(true); // Allow Outputs
               conn.setUseCaches(false); // Don't use a Cached Copy
               conn.setRequestMethod("POST");
               conn.setRequestProperty("Connection", "Keep-Alive");
               conn.setRequestProperty("ENCTYPE", "multipart/form-data");
               conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

               conn.setRequestProperty("name", pname);
               conn.setRequestProperty("categoryid", catsids);
               conn.setRequestProperty("skucode", pskucode);
               conn.setRequestProperty("sale_rate", psalerate);
               conn.setRequestProperty("purchase_rate", ppurchaserate);
               conn.setRequestProperty("weight", pweight);
               conn.setRequestProperty("weighttype", pweighttype);
               conn.setRequestProperty("description", "hello");

               conn.setRequestProperty("image", abcd.toString()); 


               dos = new DataOutputStream(conn.getOutputStream());

               dos.writeBytes(twoHyphens + boundary + lineEnd); 

               dos.writeBytes("Content-Disposition: form-data; name=\"type\""
                         + lineEnd);
                 dos.writeBytes(lineEnd);


                 dos.writeBytes(lineEnd);
                 dos.writeBytes(twoHyphens + boundary + lineEnd);*/

                 dos.writeBytes("name="+pname);
                 dos.writeBytes(lineEnd);
                 dos.writeBytes(twoHyphens + boundary + lineEnd);


                 dos.writeBytes("categoryid="+catsids);
                 dos.writeBytes(lineEnd);
                 dos.writeBytes(twoHyphens + boundary + lineEnd);

                 dos.writeBytes("skucode="+pskucode);
                 dos.writeBytes(lineEnd);
                 dos.writeBytes(twoHyphens + boundary + lineEnd);

                 dos.writeBytes("sale_rate="+psalerate);
                 dos.writeBytes(lineEnd);
                 dos.writeBytes(twoHyphens + boundary + lineEnd);

                 dos.writeBytes("purchase_rate="+ppurchaserate);
                 dos.writeBytes(lineEnd);
                 dos.writeBytes(twoHyphens + boundary + lineEnd);

                 dos.writeBytes("weight="+pweight);
                 dos.writeBytes(lineEnd);
                 dos.writeBytes(twoHyphens + boundary + lineEnd);

                 dos.writeBytes("weighttype="+pweighttype);
                 dos.writeBytes(lineEnd);
                 dos.writeBytes(twoHyphens + boundary + lineEnd);

                 dos.writeBytes("description="+"hello");
                 dos.writeBytes(lineEnd);
                 dos.writeBytes(twoHyphens + boundary + lineEnd);


               dos.writeBytes("Content-Disposition: form-data; name=\"image\";filename=\""
                                         + abcd.toString() + "\"" + lineEnd);

               dos.writeBytes(lineEnd);

               // create a buffer of  maximum size
               bytesAvailable = fileInputStream.available(); 

               bufferSize = Math.min(bytesAvailable, maxBufferSize);
               buffer = new byte[bufferSize];

               // read file and write it into form...
               bytesRead = fileInputStream.read(buffer, 0, bufferSize);  

               while (bytesRead > 0) {

                 dos.write(buffer, 0, bufferSize);
                 bytesAvailable = fileInputStream.available();
                 bufferSize = Math.min(bytesAvailable, maxBufferSize);
                 bytesRead = fileInputStream.read(buffer, 0, bufferSize);   

                }

               // send multipart form data necesssary after file data...
               dos.writeBytes(lineEnd);
               dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

               // Responses from the server (code and message)
               serverResponseCode = conn.getResponseCode();
               String serverResponseMessage = conn.getResponseMessage();

               Log.i("uploadFile", "HTTP Response is : "
                       + serverResponseMessage + ": " + serverResponseCode);

               if(serverResponseCode == 200){

                   runOnUiThread(new Runnable() {
                        public void run() {
                            String msg = "File Upload Completed.\n\n See uploaded file here : \n\n"
                                  +" F:/wamp/wamp/www/uploads";
                            messageText.setText(msg);
                            Toast.makeText(MainActivity.this, "File Upload Complete.", Toast.LENGTH_SHORT).show();
                        }
                    });                
               }    


               fileInputStream.close();
               dos.flush();
               dos.close();

          } catch (MalformedURLException ex) {

              dialog.dismiss();  
              ex.printStackTrace();

              runOnUiThread(new Runnable() {
                  public void run() {
                      messageText.setText("MalformedURLException Exception : check script url.");
                      Toast.makeText(MainActivity.this, "MalformedURLException", Toast.LENGTH_SHORT).show();
                  }
              });

              Log.e("Upload file to server", "error: " + ex.getMessage(), ex);  
          } catch (Exception e) {

              dialog.dismiss();  
              e.printStackTrace();

              runOnUiThread(new Runnable() {
                  public void run() {
                      messageText.setText("Got Exception : see logcat ");
                      Toast.makeText(MainActivity.this, "Got Exception : see logcat ", Toast.LENGTH_SHORT).show();
                  }
              });
              Log.e("Upload file to server Exception", "Exception : "  + e.getMessage(), e);  
          }
          dialog.dismiss();       
          return serverResponseCode; 

       } 
     }

}

【问题讨论】:

  • 出现问题是因为setRequestProperty 仅接受字符串作为第二个参数。所以不要传递 multimgss ArrayList 从 multimgss 创建一个 JSONArray 或 JSONObject 然后将它传递给 setRequestProperty 。在JSONArray or JSONObject 中传递图像有任何问题吗?
  • 如何使用jsonarray传递?
  • 使用conn.setRequestProperty("image", jsonarray.toString()); 传递JSONArray
  • 是的,但是如何在 jsonarray 中进行 multiimgss?我正在尝试这种方式..JSONArray abcd=new JSONArray(); abcd=multimgss.toString();..但不工作
  • 我还更新了完整代码..当我运行应用程序时它说..04-20 12:59:20.895: E/AndroidRuntime(1177): FATAL EXCEPTION: Thread-107 04-20 12: 59:20.895: E/AndroidRuntime(1177): java.lang.NullPointerException 04-20 12:59:20.895: E/AndroidRuntime(1177): 在 java.io.File.fixSlashes(File.java:185) 04-20 12:59:20.895:E/AndroidRuntime(1177):在 java.io.File.(File.java:134)

标签: android json android-imageview multipartentity http-request-parameters


【解决方案1】:

您可以将String 参数作为setRequestProperty() 的两个字段发送

问题:在第二个参数中,您传递的是ArrayList&lt;String&gt;

尝试将multimgss的每个元素与setRequestProperty()一一传递

               FileInputStream fileInputStream = new FileInputStream(sourceFile);
           URL url = new URL(upLoadServerUri);

           // Open a HTTP  connection to  the URL
           conn = (HttpURLConnection) url.openConnection(); 
           conn.setDoInput(true); // Allow Inputs
           conn.setDoOutput(true); // Allow Outputs
           conn.setUseCaches(false); // Don't use a Cached Copy
           conn.setRequestMethod("POST");
           conn.setRequestProperty("Connection", "Keep-Alive");
           conn.setRequestProperty("ENCTYPE", "multipart/form-data");
           conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

           conn.setRequestProperty("name", pname);
           conn.setRequestProperty("categoryid", catsids);
           conn.setRequestProperty("skucode", pskucode);
           conn.setRequestProperty("sale_rate", psalerate);
           conn.setRequestProperty("purchase_rate", ppurchaserate);
           conn.setRequestProperty("weight", pweight);
           conn.setRequestProperty("weighttype", pweighttype);
           conn.setRequestProperty("description", pdesc);


          conn.setRequestProperty("image", multimgss.get(i++));   
           // it will give i th element 

注意:来自setRequestProperty() 的 Android 开发者文档

设置指定请求头域的值。该值将仅由当前 URLConnection 实例使用。该方法只能在连接建立之前调用。

所以,每次建立连接之前都需要调用这个方法

【讨论】:

  • 您遇到了什么问题?您应该在 setRequestProperty.. 中将 String 作为第二个参数传递。你也在做同样的事情吗?
  • 我的完整代码太长了,否则我肯定会在这里发布
  • 查看我更新的代码..i sysout multimgss 并在我的 logcat 中显示..multipleimagesss[null]
【解决方案2】:

setRequestProperty 方法只接受 String 而不是 ArrayList 。从ArrayList 获取JSONArray 为:

JSONArray jsonArrayImgs=new JSONArray();
 ArrayList<String> multimgss=new ArrayList<String>();
 for (String string : multimgss)
  {
    jsonArrayImgs.put(string);
  }

现在将jsonArrayImgs 传递给setRequestProperty

conn.setRequestProperty("image", jsonArrayImgs.toString()); 

编辑:

sourceFileUrinull 。所以从适配器更新getItem 方法:

public String getItem(int position) {
       return arrayList.get[position];
   }

现在在GridViewonItemClick 中将选定的图像路径分配给sourceFileUri 为:

public void onItemClick(AdapterView<?> parent, 
        View v, int position, long id) 
        {                
             imagepath=objImageAdapter.getItem(position);
        }

【讨论】:

  • 应用程序崩溃并显示错误请参阅我发布的 logcat 编辑问题
  • @chris: 290 的行号是多少?
  • 仍然没有变化..同样的错误和同一行 File sourceFile = new File(sourceFileUri);
  • @chris: 现在有什么问题?
  • 与我的问题 logcat image 中提到的相同问题..你能告诉我什么时候我 sysout multimgss 它显示为 null..
猜你喜欢
  • 1970-01-01
  • 2013-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-25
  • 2011-12-03
相关资源
最近更新 更多