6 package com.google.appinventor.components.runtime;
8 import android.os.Handler;
24 import org.apache.http.HttpEntity;
25 import org.apache.http.HttpResponse;
26 import org.apache.http.client.HttpClient;
27 import org.apache.http.client.methods.HttpPost;
28 import org.apache.http.entity.mime.HttpMultipartMode;
29 import org.apache.http.entity.mime.MultipartEntityBuilder;
30 import org.apache.http.entity.mime.content.FileBody;
31 import org.apache.http.impl.client.DefaultHttpClient;
32 import org.apache.http.util.EntityUtils;
34 import java.io.BufferedReader;
36 import java.io.FileNotFoundException;
37 import java.io.InputStreamReader;
39 import java.net.HttpURLConnection;
63 @DesignerComponent(version = YaVersion.MEDIASTORE_COMPONENT_VERSION,
64 description =
"Non-visible component that communicates with a Web service and stores media " +
66 category = ComponentCategory.INTERNAL,
68 iconName =
"images/mediastore.png")
70 @UsesPermissions(permissionNames =
"android.permission.INTERNET")
71 @UsesLibraries(
"httpmime.jar")
74 private static final String LOG_TAG_COMPONENT =
"MediaStore: ";
75 private String serviceURL;
76 private Handler androidUIHandler;
84 super(container.
$form());
85 componentContainer = container;
86 androidUIHandler =
new Handler();
87 serviceURL =
"http://ai-mediaservice.appspot.com";
94 public String ServiceURL() {
103 defaultValue =
"http://ai-mediaservice.appspot.com")
115 public void PostMedia(String mediafile)
throws FileNotFoundException{
117 public void onSuccess(
final String response) {
118 androidUIHandler.post(
new Runnable() {
120 MediaStored(response);
124 public void onFailure(
final String message) {
125 androidUIHandler.post(
new Runnable() {
127 WebServiceError(message);
134 HttpClient client =
new DefaultHttpClient();
136 MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
137 entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
139 String[] pathtokens = mediafile.split(
"/");
142 if (pathtokens[0].equals(
"file:")) {
143 newMediaPath =
new java.io.File(
new URL(mediafile).toURI()).getAbsolutePath();
145 newMediaPath = mediafile;
148 File media =
new File(newMediaPath);
149 entityBuilder.addPart(
"file",
new FileBody(media));
151 HttpEntity entity = entityBuilder.build();
153 String uploadURL = getUploadUrl();
154 HttpPost post =
new HttpPost(uploadURL);
155 post.setEntity(entity);
156 HttpResponse response = client.execute(post);
158 HttpEntity httpEntity = response.getEntity();
159 String result = EntityUtils.toString(httpEntity);
161 }
catch (Exception e) {
167 private String getUploadUrl() {
169 String url = serviceURL;
170 URL obj =
new URL(url);
172 HttpURLConnection con = (HttpURLConnection) obj.openConnection();
175 con.setRequestMethod(
"GET");
178 String USER_AGENT =
"AppInventor";
179 con.setRequestProperty(
"User-Agent", USER_AGENT);
180 con.setRequestProperty(
"Content-Type",
"text/plain; charset=utf-8");
183 BufferedReader in =
new BufferedReader(
new InputStreamReader(con.getInputStream()));
184 StringBuilder response =
new StringBuilder();
186 while ((inputLine = in.readLine()) !=
null) {
187 response.append(inputLine);
192 return response.toString();
193 }
catch (Exception e) {