我正在尝试将图像文件 (PNG) 发布到 php 脚本,以便我可以从 $_FILES 中获取它们,但是,当我尝试提交数据以上传我的 android 应用程序时,我只是在那里呆了很长时间,我得到了一个不来自系统的响应消息。文件上传时间太长还是我做错了什么?
public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("PHP_SCRIPT_URL");
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
String term = sharedPref.getString(getString(R.string.current_term), "000");
String location = sharedPref.getString(getString(R.string.current_location), "000");
EditText text1 = (EditText) this.findViewById(R.id.entry_title);
EditText text2 = (EditText) this.findViewById(R.id.entry_comments);
String title = text1.getText().toString();
String comment = text2.getText().toString();
MultipartEntity multi = new MultipartEntity();
try {
// Add your data
multi.addPart("term", new StringBody(term));
multi.addPart("space", new StringBody(location));
multi.addPart("title", new StringBody(title));
multi.addPart("comment", new StringBody(comment));
File cacheDir = new File(this.getCacheDir(), "thumbnails");
if (cacheDir.exists()) {
File cacheFile;
for (int i = 0; i < 6; i++) {
cacheFile = new File(cacheDir, "photo_" + i);
if (cacheFile.exists()) {
multi.addPart("media"+(i+1), new InputStreamBody(new FileInputStream(cacheFile), "photo_"+i));
}
}
}
httppost.setEntity(multi);
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
int responseCode = response.getStatusLine().getStatusCode();
switch(responseCode) {
case 200:
check_success = true;
break;
default:
check_success = false;
new AlertDialog.Builder(this).setTitle("ERROR").setMessage("HTTP RESPONSE: "+responseCode).setNeutralButton("Close", null).show();
}
} catch (ClientProtocolException e) {
new AlertDialog.Builder(this).setTitle("ERROR").setMessage("ClientProtocolException\n" + e.getMessage()).setNeutralButton("Close", null).show();
} catch (IOException e) {
new AlertDialog.Builder(this).setTitle("ERROR").setMessage("IOException\n" + e.getMessage()).setNeutralButton("Close", null).show();
}
}