在类型的URLConnection方法调用setRequestProperty(字符串,字符串)是不适用的参数(字符串,ArrayList的&其中;字符串&GT)(The method setRequ
I am trying to send multiple images to server but,so I am storing all the images in one arraylist,but after that when I need to send to server,it shows error near line ,,......................
conn.setRequestProperty("image", multimgss);
error
The method setRequestProperty(String, String) in the type URLConnection is not applicable for the arguments (String, ArrayList<String>)..
MainActivity
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 = "rn";
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.nn See uploaded file here : nn"
+" F:/wamp/wamp/www/uploads";
messageText.setText(msg);
Toast.makeText(MainActivity.this, "File Upload Complete.", Toast.LENGTH_SHORT).show();
}
});
}
Fullcode
public class MainActivity extends Activity implements 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 = "rn";
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.nn See uploaded file here : nn"
+" 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 method only accept String instead of ArrayList . get JSONArray from ArrayList as:
JSONArray jsonArrayImgs=new JSONArray();
ArrayList<String> multimgss=new ArrayList<String>();
for (String string : multimgss)
{
jsonArrayImgs.put(string);
}
Now pass jsonArrayImgs to setRequestProperty :
conn.setRequestProperty("image", jsonArrayImgs.toString());
EDIT:
sourceFileUri is null . so update getItem method as of Adapter:
public String getItem(int position) {
return arrayList.get[position];
}
now inside onItemClick of GridView asign selected image path to sourceFileUri as:
public void onItemClick(AdapterView<?> parent,
View v, int position, long id)
{
imagepath=objImageAdapter.getItem(position);
}
我想多张图片发送到服务器,但是,所以我存储的所有图像在一个ArrayList中,但在那之后,当我需要发送到服务器,它显示近线错误,, ....... ...............
conn.setRequestProperty(形象,multimgss);
错误
在类型的URLConnection的方法调用setRequestProperty(字符串,字符串)不适用的参数(字符串,ArrayList的&LT;字符串&GT;)..
MainActivity
公众诠释uploadFile(字符串sourceFileUri){
字符串文件名= sourceFileUri;
/ *的String [] mimgs = NULL;
mimgs.equals(文件名); * /
ArrayList的&LT;字符串&GT; multimgss =新的ArrayList&LT;字符串&GT;();
multimgss.add(文件名);
的System.out.println(multipleimagesss+ multimgss);
尝试 {
ABCD =新JSONArray(multimgss.toString());
}赶上(JSONException E1){
// TODO自动生成的catch块
e1.printStackTrace();
}
//abcd=multimgss.add(fileName);
HttpURLConnection的康恩= NULL;
DataOutputStream类DOS = NULL;
字符串lineEnd = r N的;
串twoHyphens = - ;
字符串边界=*****;
INT读取动作,方bytesAvailable,缓冲区大小;
byte []的缓冲区;
INT maxBufferSize = 1 * 1024 * 1024;
文件的SourceFile =新的文件(sourceFileUri);
如果(!sourceFile.isFile()){
dialog.dismiss();
Log.e(一个UploadFile,源文件不存在:+的ImagePath);
runOnUiThread(新的Runnable(){
公共无效的run(){
messageText.setText(源文件不存在:+的ImagePath);
}
});
返回0;
}
其他
{
尝试 {
串猫[] = acTextView.getText()的toString()分裂()。;
JsonParse JP =新JsonParse();
对于(字符串的猫:猫){
名单&LT; SuggestGetSet&GT;表= jp.getParseJsonWCF(acTextView.getText()的toString());
的for(int i = 0; I&LT;则为list.size();我++){
如果(list.get(ⅰ).getName()。等于(猫))
catid.add(list.get(ⅰ).getId()的toString());
//params.add(new BasicNameValuePair(PARENTID,list.get(ⅰ).getId()));
}
}
catsids = catid.toString();
PNAME = proname.getText()的toString()。
psku code = prosku code.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(活性NU stauts+ catsids);
// pdisporder = prodisporder.getText()的toString();
pdesc = prodesc.getText()的toString()。
upLoadServerUri = "http://webistname/web-service/addproduct.php?action=addproduct&name="+pname+"&categoryid="+catsids+"&sku$c$c="+psku$c$c+"&sale_rate="+psalerate+"&purchase_rate="+ppurchaserate+"&weight="+pweight+"&weighttype="+pweighttype+"&description=hello&image="+abcd;
//打开一个URL连接到这个Servlet
的FileInputStream的FileInputStream =新的FileInputStream(的资源文件);
网址URL =新的URL(upLoadServerUri);
//打开一个HTTP连接网址
康恩=(HttpURLConnection类)url.openConnection();
conn.setDoInput(真正的); //允许输入
conn.setDoOutput(真正的); //允许输出
conn.setUseCaches(假); //不要使用缓存副本
conn.setRequestMethod(POST);
conn.setRequestProperty(连接,保持活动);
conn.setRequestProperty(ENCTYPE,多部分/表单数据);
conn.setRequestProperty(内容类型,多部分/格式数据;边界=+界);
conn.setRequestProperty(名,PNAME);
conn.setRequestProperty(类别ID,catsids);
conn.setRequestProperty(SKU code,psku code);
conn.setRequestProperty(sale_rate,psalerate);
conn.setRequestProperty(purchase_rate,ppurchaserate);
conn.setRequestProperty(权重,pweight);
conn.setRequestProperty(weighttype,pweighttype);
conn.setRequestProperty(说明,你好);
// conn.setRequestProperty(displayorder,pdisporder);
// conn.setRequestProperty(主动,pproactives);
conn.setRequestProperty(形象,abcd.toString());
DOS =新DataOutputStream类(conn.getOutputStream());
dos.writeBytes(twoHyphens +边界+ lineEnd);
dos.writeBytes(内容处置:表格数据;名称= 类型
+ lineEnd);
dos.writeBytes(lineEnd);
//分配值
/*dos.writeBytes("version=apps);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ lineEnd); * /
dos.writeBytes(NAME =+ PNAME);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ lineEnd);
dos.writeBytes(类别ID =+ catsids);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ lineEnd);
dos.writeBytes(SKU code =+ psku code);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ lineEnd);
dos.writeBytes(sale_rate =+ psalerate);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ lineEnd);
dos.writeBytes(purchase_rate =+ ppurchaserate);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ lineEnd);
dos.writeBytes(重量=+ pweight);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ lineEnd);
dos.writeBytes(weighttype =+ pweighttype);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ lineEnd);
dos.writeBytes(说明=+你好);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ lineEnd);
/ * dos.writeBytes(displayorder =+ pdisporder);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ lineEnd);
dos.writeBytes(活性=+ pproactives);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ lineEnd); * /
dos.writeBytes(内容处置:表格数据;名称= 图像文件名=
+ abcd.toString()++ lineEnd);
dos.writeBytes(lineEnd);
//创建最大大小的缓冲区
方bytesAvailable = fileInputStream.available();
BUFFERSIZE = Math.min(方bytesAvailable,maxBufferSize);
缓冲区=新的字节[BUFFERSIZE]
//读取文件,并将其写入形式...
读取动作= fileInputStream.read(缓冲液,0,BUFFERSIZE);
而(读取动作大于0){
dos.write(缓冲液,0,BUFFERSIZE);
方bytesAvailable = fileInputStream.available();
BUFFERSIZE = Math.min(方bytesAvailable,maxBufferSize);
读取动作= fileInputStream.read(缓冲液,0,BUFFERSIZE);
}
//发送多部分表格数据文件数据后necesssary ...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ twoHyphens + lineEnd);
从服务器//回应(code和消息)
serverResponse code = conn.getResponse code();
串serverResponseMessage = conn.getResponseMessage();
Log.i(一个UploadFile,HTTP响应是:
+ serverResponseMessage +:+ serverResponse code);
如果(serverResponse code == 200){
runOnUiThread(新的Runnable(){
公共无效的run(){
弦乐味精=文件上传已完成ññ看到这里上传的文件:ñ N
+F:/ WAMP / WAMP / WWW /上传;
messageText.setText(MSG);
Toast.makeText(MainActivity.this,文件上传完成。,Toast.LENGTH_SHORT).show();
}
});
}
全部code
公共类MainActivity扩展活动实现OnClickListener {
私有静态诠释RESULT_LOAD_IMAGE = 1;
私人TextView中的MessageText;
私人按钮uploadButton;
私人ImageView的ImageView的;
私人诠释serverResponse code = 0;
私人ProgressDialog对话框= NULL;
私人字符串upLoadServerUri = NULL;
私人字符串的ImagePath = NULL;
私人的EditText proname;
私人的EditText prosku code;
私人的EditText prodesc;
//私人复选框proactives;
私人ImageView的btnselectpic;
私人字符串psku code;
私人的EditText prosalerate;
私人的EditText propurchaserate;
私人的EditText proweight;
私人的EditText proweighttype;
//私人的EditText prodisporder;
私人字符串PNAME;
私人字符串psalerate;
私人字符串ppurchaserate;
私人字符串pweight;
私人字符串pweighttype;
私人字符串pproactives;
私人字符串pdisporder;
私人字符串pdesc;
私人列表&LT;字符串&GT; CATID =新的ArrayList&LT;字符串&GT;();
私人AutoCompleteTextView acTextView;
私人字符串catsids;
私人乌里selectedImageUri;
私人诠释参数:columnIndex;
私人ImageAdapter objImageAdapter;
私人GridView控件GridView控件;
私人JSONArray ABCD;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
GridView控件=(GridView控件)findViewById(R.id.gridview);
//gridView.setAdapter(new ImageAdapter(本));
objImageAdapter =新ImageAdapter(本);
gridView.setAdapter(objImageAdapter);
gridView.setOnItemClickListener(新OnItemClickListener()
{
公共无效onItemClick(适配器视图&LT;&GT;父,
视图V,INT位置,长ID)
{
Toast.makeText(getBaseContext(),
PIC+(位置+ 1)+选择,
Toast.LENGTH_SHORT).show();
}
});
acTextView =(AutoCompleteTextView)findViewById(R.id.multautoComplete);
acTextView.setAdapter(新SuggestionAdapter(此,acTextView.getText()的toString())。);
// acTextView.setTokenizer(新MultiAutoCompleteTextView.CommaTokenizer());
proname =(EditText上)findViewById(R.id.edt_proname);
prosku code =(EditText上)findViewById(R.id.edt_prosku code);
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 =(复选框)findViewById(R.id.edt_proactive);
uploadButton =(按钮)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(本);
uploadButton.setOnClickListener(本);
}
@覆盖
公共无效的onClick(查看为arg0){
如果(将arg0 == btnselectpic)
{
/ *意向意图=新的意图(MainActivity.this,PhotoSelection.class);
startActivity(意向); * /
意向意图=新的意图(Intent.ACTION_GET_CONTENT);
intent.setType(图像/ *);
startActivityForResult(意向,1);
/ *意向意图=新的意图();
intent.setType(图像/ *);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(原意,用完整的行动),1); * /
/ *意图I =新的意图(Action.ACTION_MULTIPLE_PICK);
startActivityForResult(ⅰ,200); * /
}
否则,如果(为arg0 == uploadButton){
对话框= ProgressDialog.show(MainActivity.this,,上传文件...,真正的);
messageText.setText(上传开始......);
新主题(新的Runnable(){
公共无效的run(){
uploadFile(的ImagePath);
}
})。开始();
}
}
@覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
如果(要求code == RESULT_LOAD_IMAGE和放大器;&安培;结果code == RESULT_OK和放大器;&安培;!NULL =数据){
selectedImageUri = data.getData();
的String [] filePathColumn = {MediaStore.Images.Media.DATA};
光标光标= getContentResolver()查询(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.de codeFILE(的ImagePath));
参数:columnIndex = cursor.getColumnIndex(filePathColumn [0]);
字符串picturePath = cursor.getString(参数:columnIndex);
objImageAdapter.addToList(picturePath);
cursor.close();
// addmores.setVisibility(View.VISIBLE);
}
/ *如果(要求code == 1安培;&安培;结果code == RESULT_OK){
//位图的照片=(位图)data.getData()getPath()。
selectedImageUri = data.getData();
的ImagePath = getPath(selectedImageUri);
点阵位图= BitmapFactory.de codeFILE(的ImagePath);
imageview.setImageBitmap(位);
messageText.setText(上传文件路径:+的ImagePath);
} * /
}
公共字符串getPath(URI URI){
的String []投影= {MediaStore.Images.Media.DATA};
光标光标= managedQuery(URI,投影,NULL,NULL,NULL);
INT与Column_Index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
返回cursor.getString(Column_Index中);
}
公共类ImageAdapter扩展了BaseAdapter
{
私人上下文的背景下;
ArrayList的&LT;字符串&GT; ArrayList的=新的ArrayList&LT;字符串&GT;();
公共ImageAdapter(上下文C)
{
上下文= C;
}
// ---返回图像的数量---
公众诠释getCount将(){
返回arrayList.size();
}
// ---返回一个项目的ID ---
公共对象的getItem(INT位置){
返回的位置;
}
无效addToList(字符串strPath中)
{
this.arrayList.add(strPath的);
this.notifyDataSetChanged();
}
众长getItemId(INT位置){
返回的位置;
}
//在这个数组,你必须保存所有图片的路径是要在baseapater显示,并且必须是全球性的baseapater访问
公共查看getView(INT位置,查看convertView,父母的ViewGroup)
{
ImageView的ImageView的;
如果(convertView == NULL){
ImageView的=新ImageView的(上下文);
imageView.setLayoutParams(新GridView.LayoutParams(185,185));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5,5,5,5);
} 其他 {
ImageView的=(ImageView的)convertView;
}
字符串路径= arrayList.get(位置);
位图MYBITMAP = BitmapFactory.de codeFILE(路径);
imageView.setImageBitmap(MYBITMAP);
返回ImageView的;
}
}
公众诠释uploadFile(字符串sourceFileUri){
字符串文件名= sourceFileUri;
/ *的String [] mimgs = NULL;
mimgs.equals(文件名); * /
ArrayList的&LT;字符串&GT; multimgss =新的ArrayList&LT;字符串&GT;();
multimgss.add(文件名);
的System.out.println(multipleimagesss+ multimgss);
尝试 {
ABCD =新JSONArray(multimgss.toString());
}赶上(JSONException E1){
// TODO自动生成的catch块
e1.printStackTrace();
}
//abcd=multimgss.add(fileName);
HttpURLConnection的康恩= NULL;
DataOutputStream类DOS = NULL;
字符串lineEnd = r N的;
串twoHyphens = - ;
字符串边界=*****;
INT读取动作,方bytesAvailable,缓冲区大小;
byte []的缓冲区;
INT maxBufferSize = 1 * 1024 * 1024;
文件的SourceFile =新的文件(sourceFileUri);
如果(!sourceFile.isFile()){
dialog.dismiss();
Log.e(一个UploadFile,源文件不存在:+的ImagePath);
runOnUiThread(新的Runnable(){
公共无效的run(){
messageText.setText(源文件不存在:+的ImagePath);
}
});
返回0;
}
其他
{
尝试 {
串猫[] = acTextView.getText()的toString()分裂()。;
JsonParse JP =新JsonParse();
对于(字符串的猫:猫){
名单&LT; SuggestGetSet&GT;表= jp.getParseJsonWCF(acTextView.getText()的toString());
的for(int i = 0; I&LT;则为list.size();我++){
如果(list.get(ⅰ).getName()。等于(猫))
catid.add(list.get(ⅰ).getId()的toString());
//params.add(new BasicNameValuePair(PARENTID,list.get(ⅰ).getId()));
}
}
catsids = catid.toString();
PNAME = proname.getText()的toString()。
psku code = prosku code.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(活性NU stauts+ catsids);
// pdisporder = prodisporder.getText()的toString();
pdesc = prodesc.getText()的toString()。
upLoadServerUri = "http://mywebsitename/web-service/addproduct.php?action=addproduct&name="+pname+"&categoryid="+catsids+"&sku$c$c="+psku$c$c+"&sale_rate="+psalerate+"&purchase_rate="+ppurchaserate+"&weight="+pweight+"&weighttype="+pweighttype+"&description=hello&image="+abcd;
//打开一个URL连接到这个Servlet
的FileInputStream的FileInputStream =新的FileInputStream(的资源文件);
网址URL =新的URL(upLoadServerUri);
//打开一个HTTP连接网址
康恩=(HttpURLConnection类)url.openConnection();
conn.setDoInput(真正的); //允许输入
conn.setDoOutput(真正的); //允许输出
conn.setUseCaches(假); //不要使用缓存副本
conn.setRequestMethod(POST);
conn.setRequestProperty(连接,保持活动);
conn.setRequestProperty(ENCTYPE,多部分/表单数据);
conn.setRequestProperty(内容类型,多部分/格式数据;边界=+界);
conn.setRequestProperty(名,PNAME);
conn.setRequestProperty(类别ID,catsids);
conn.setRequestProperty(SKU code,psku code);
conn.setRequestProperty(sale_rate,psalerate);
conn.setRequestProperty(purchase_rate,ppurchaserate);
conn.setRequestProperty(权重,pweight);
conn.setRequestProperty(weighttype,pweighttype);
conn.setRequestProperty(说明,你好);
conn.setRequestProperty(形象,abcd.toString());
DOS =新DataOutputStream类(conn.getOutputStream());
dos.writeBytes(twoHyphens +边界+ lineEnd);
dos.writeBytes(内容处置:表格数据;名称= 类型
+ lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ lineEnd); * /
dos.writeBytes(NAME =+ PNAME);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ lineEnd);
dos.writeBytes(类别ID =+ catsids);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ lineEnd);
dos.writeBytes(SKU code =+ psku code);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ lineEnd);
dos.writeBytes(sale_rate =+ psalerate);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ lineEnd);
dos.writeBytes(purchase_rate =+ ppurchaserate);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ lineEnd);
dos.writeBytes(重量=+ pweight);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ lineEnd);
dos.writeBytes(weighttype =+ pweighttype);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ lineEnd);
dos.writeBytes(说明=+你好);
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ lineEnd);
dos.writeBytes(内容处置:表格数据;名称= 图像文件名=
+ abcd.toString()++ lineEnd);
dos.writeBytes(lineEnd);
//创建最大大小的缓冲区
方bytesAvailable = fileInputStream.available();
BUFFERSIZE = Math.min(方bytesAvailable,maxBufferSize);
缓冲区=新的字节[BUFFERSIZE]
//读取文件,并将其写入形式...
读取动作= fileInputStream.read(缓冲液,0,BUFFERSIZE);
而(读取动作大于0){
dos.write(缓冲液,0,BUFFERSIZE);
方bytesAvailable = fileInputStream.available();
BUFFERSIZE = Math.min(方bytesAvailable,maxBufferSize);
读取动作= fileInputStream.read(缓冲液,0,BUFFERSIZE);
}
//发送多部分表格数据文件数据后necesssary ...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ twoHyphens + lineEnd);
从服务器//回应(code和消息)
serverResponse code = conn.getResponse code();
串serverResponseMessage = conn.getResponseMessage();
Log.i(一个UploadFile,HTTP响应是:
+ serverResponseMessage +:+ serverResponse code);
如果(serverResponse code == 200){
runOnUiThread(新的Runnable(){
