/[projects]/android/PicturePoster/src/dk/thoerup/pictureposter/PostActivity.java
ViewVC logotype

Contents of /android/PicturePoster/src/dk/thoerup/pictureposter/PostActivity.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 596 - (show annotations) (download)
Sat Feb 20 12:53:56 2010 UTC (14 years, 3 months ago) by torben
File size: 4604 byte(s)
Check response code
1 package dk.thoerup.pictureposter;
2
3 import java.io.File;
4 import java.nio.charset.Charset;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import org.apache.http.HttpResponse;
9 import org.apache.http.NameValuePair;
10 import org.apache.http.client.HttpClient;
11 import org.apache.http.client.methods.HttpPost;
12 import org.apache.http.client.utils.URLEncodedUtils;
13 import org.apache.http.entity.mime.MultipartEntity;
14 import org.apache.http.entity.mime.content.FileBody;
15 import org.apache.http.entity.mime.content.StringBody;
16 import org.apache.http.impl.client.DefaultHttpClient;
17 import org.apache.http.message.BasicNameValuePair;
18
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.database.Cursor;
22 import android.net.Uri;
23 import android.os.Bundle;
24 import android.util.Log;
25 import android.view.View;
26 import android.view.View.OnClickListener;
27 import android.widget.Button;
28 import android.widget.ImageView;
29 import android.widget.TextView;
30 import android.widget.Toast;
31
32 public class PostActivity extends Activity {
33 static final int IMAGE_PICK = 1000;
34
35 ImageView picture;
36 String media_path;
37 TextView title;
38 TextView note;
39
40 /** Called when the activity is first created. */
41 @Override
42 public void onCreate(Bundle savedInstanceState) {
43 super.onCreate(savedInstanceState);
44 setContentView(R.layout.main);
45
46 picture = (ImageView) findViewById(R.id.picture);
47 title = (TextView) findViewById(R.id.title);
48 note = (TextView) findViewById(R.id.note);
49
50 Button changePicture = (Button) findViewById(R.id.changePicture);
51 changePicture.setOnClickListener( new OnClickListener() {
52 public void onClick(View v) {
53 changePicture();
54 }
55 });
56
57 Button submit = (Button) findViewById(R.id.submit);
58 submit.setOnClickListener( new OnClickListener() {
59 public void onClick(View v) {
60 submit();
61 }
62 });
63 }
64
65
66
67
68 @Override
69 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
70 super.onActivityResult(requestCode, resultCode, data);
71
72 switch(requestCode) {
73 case IMAGE_PICK:
74 imagePicked(resultCode,data);
75 break;
76 }
77
78 }
79
80
81 void imagePicked(int resultCode,Intent data) {
82 Log.i("PostActivity", "ResultCode " + resultCode);
83
84 if (resultCode != 0) {
85 Log.i("PostActivity", data.getDataString());
86 Cursor c = managedQuery(data.getData(),null,null,null,null) ;
87
88 if( c.moveToFirst() ) {
89 media_path = c.getString(1) ;
90 Log.i("PostActivity", media_path);
91 picture.setImageURI( Uri.parse(media_path) );
92 } else {
93 picture.setImageDrawable(null);
94 }
95 }
96 }
97
98 void changePicture() {
99
100 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
101 intent.setType("image/*");
102 startActivityForResult(intent, IMAGE_PICK);
103 }
104
105 void submit() {
106 HttpPost post = new HttpPost("http://app.t-hoerup.dk/PicturePosterService/PostServlet");
107 String titleStr = title.getText().toString();
108 String noteStr = note.getText().toString();
109
110 if (titleStr.trim().length() == 0) {
111 Toast.makeText(this, "Please enter a title", Toast.LENGTH_LONG).show();
112 return;
113 }
114 if (noteStr.trim().length() == 0) {
115 Toast.makeText(this, "Please enter a note", Toast.LENGTH_LONG).show();
116 return;
117 }
118
119 if (media_path == null || media_path.length() == 0) {
120 Toast.makeText(this, "Please select an picture", Toast.LENGTH_LONG).show();
121 return;
122 }
123
124
125
126 MultipartEntity entity = new MultipartEntity();
127
128 entity.addPart("file", new FileBody( new File(media_path), "image/jpeg") );
129
130 try {
131 List<NameValuePair> data = new ArrayList<NameValuePair>();
132 data.add( new BasicNameValuePair("title", titleStr) );
133 data.add( new BasicNameValuePair("note", noteStr) );
134 String encoded = URLEncodedUtils.format(data, "UTF-8");
135 entity.addPart("text", new StringBody(encoded, URLEncodedUtils.CONTENT_TYPE, Charset.forName("UTF-8") ));
136 } catch (Exception e) {
137 Log.e("asdasd","asdasdasd",e);
138 }
139
140
141 post.setEntity(entity);
142 boolean ok = false;
143 HttpClient client = new DefaultHttpClient();
144 try {
145 HttpResponse resp = client.execute(post);
146 if (resp.getStatusLine().getStatusCode() == 200) {
147 ok = true;
148 }
149
150 } catch (Exception e) {
151 Log.d("PostActivity", "upload failed", e);
152
153 }
154 if (ok == true) {
155 Toast.makeText(this, "Upload succeeded", Toast.LENGTH_LONG).show();
156 } else {
157 Toast.makeText(this, "Upload failed", Toast.LENGTH_LONG).show();
158 }
159 }
160
161 }

  ViewVC Help
Powered by ViewVC 1.1.20