对Android后台java代码做出一些改动,在使用HttpURLConnection传递多个参数,即传递图片,以及传递图+文方面出现问题。由于满课原因,servlet未能构建完成。
下面给出代码:
增设定义:
1 private EditText note; // 笔记文本 2 private EditText title; // 标题文本 3 private Switch see; // 是否公开 4 private Button handin; // 上传按钮
昨日上传图片板块改动:
gridview文件的imageview改动:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 6 <ImageView 7 android:id="@+id/imageView1" 8 android:layout_width="wrap_content" 9 android:layout_height="90dp" 10 ></ImageView> 11 </LinearLayout>
高度设置90dp,效果:
图片相关后台代码改动:
1 public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 2 // TODO Auto-generated method stub 3 if (imageItem.size() == 4&&position==0) { // 第一张为默认图片,点击+号时才判定是否已满 4 Toast.makeText(MainActivity.this, "图片数3张已满", 5 Toast.LENGTH_SHORT).show(); 6 } else if (position == 0) { // 点击图片位置为+ 0对应0张图片 7 // 选择图片 8 dialog.show(); 9 10 // 通过onResume()刷新数据 11 } else { 12 dialog(position); 13 } 14 15 }
原代码在图片满3张之后,不论点哪一张都会显示已满,不符合我们的要求,按如上改动之后,它会根据position值判定是否点击的是+号,效果如下:
最后是疑问部分代码:
1 private void init() { 2 note = (EditText)findViewById(R.id.note); 3 gridView = (GridView) findViewById(R.id.gridView); 4 handin = (Button)findViewById(R.id.handin); 5 title = (EditText)findViewById(R.id.title); 6 see = (Switch)findViewById(R.id.see); 7 note.setHorizontallyScrolling(true); 8 gridView.setOnItemClickListener(this); 9 dialog = new MyDialog(this); 10 dialog.setOnButtonClickListener(this); 11 // activity中调用其他activity中组件的方法 12 LayoutInflater layout = this.getLayoutInflater(); 13 View view = layout.inflate(R.layout.layout_select_photo, null); 14 15 handin.setOnClickListener(new View.OnClickListener() { 16 @Override 17 public void onClick(View v) { 18 //正文信息 19 final String note_str = note.getText().toString(); 20 //标题信息 21 final String title_str = title.getText().toString(); 22 final int[] see_judge={}; 23 24 //根据开关决定笔记是否公开,0为公开,1为不公开 25 see.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 26 @Override 27 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 28 if(isChecked){ 29 see_judge[0] = 0; 30 } 31 else{ 32 see_judge[0] = 1; 33 } 34 } 35 }); 36 37 //HttpURLConnection 38 final String path="http://...../.../Servlet"; 39 new Thread(new Runnable() { 40 @Override 41 public void run() { 42 HttpURLConnection conn = null; 43 try{ 44 URL url = new URL(path); 45 conn = (HttpURLConnection)url.openConnection(); 46 conn.setRequestMethod("POST"); 47 conn.setConnectTimeout(5000); 48 conn.setReadTimeout(5000); 49 conn.setUseCaches(false); 50 conn.setDoInput(true); 51 //??? 52 String note_S = "note="+ URLEncoder.encode(note_str,"UTF-8"); 53 String title_S = "title="+URLEncoder.encode(title_str,"UTF-8"); 54 String see_S = "see="+see_judge[0]; 55 String data=note_S+"&"+title_S+"&"+see_S; 56 //图片-file? 57 imageItem 58 }catch (Exception e){ 59 e.printStackTrace(); 60 } 61 } 62 }).start(); 63 } 64 }); 65 }
明日任务:攻克疑问部分。