0
當用戶點擊按鈕時,我嘗試發送一組編輯文本和PDF文件到服務器。我成功地發送了一組文本沒有pdf,然後我成功地發送PDF文本。我想在同一時間發送給他們兩人在同一個URL如何做到這一點的文字 代碼從Android應用程序發送文本和PDF文件到服務器
private class postData extends AsyncTask<String, Void, String> {
// private final ProgressDialog dialog = ProgressDialog.show(getActivity(), "",
// "Saving data to server. Please wait...", true);
EditText email = (EditText) myView.findViewById(R.id.email);
EditText phone = (EditText) myView.findViewById(R.id.email);
String eemail=email.getText().toString();
String pphone=phone.getText().toString();
EditText first = (EditText) myView.findViewById(R.id.firstname);
EditText second= (EditText) myView.findViewById(R.id.lastname);
String firstname=first.getText().toString();
String lastname=second.getText().toString();
@Override
protected String doInBackground(String... params) {
// perform long running operation operation
// SharedPreferences settings = context.getSharedPreferences(PREFS_FILE, 0);
//String server = settings.getString("server", "");
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://phone.tmsline.com/api/request_job");
String json = "";
String responseStr="";
String result="true";
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("email", eemail));
nameValuePairs.add(new BasicNameValuePair("phone", pphone));
nameValuePairs.add(new BasicNameValuePair("cv","not now"));
nameValuePairs.add(new BasicNameValuePair("firstname", firstname));
nameValuePairs.add(new BasicNameValuePair("lastname", lastname));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
try {
httpclient.execute(httppost);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} catch (IOException e) {
// TODO Auto-generated catch block
Log.i("HTTP Failed", e.toString());
}
return result;
}
protected void onPostExecute(String responseStr) {
super.onPostExecute(responseStr);
Toast.makeText(getActivity(),responseStr,Toast.LENGTH_LONG).show();
if(responseStr.equals("true")){
// Update your Button here
Toast.makeText(getActivity(),"done finally",Toast.LENGTH_LONG).show();
}
}
}
PDF的代碼,而不是使用所有這一切,最好的
public void filetest(){
String url = "http://phone.tmsline.com/api/";
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),
"Sherouk adel.pdf");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
InputStreamEntity reqEntity = new InputStreamEntity(
new FileInputStream(file), -1);
reqEntity.setContentType("binary/octet-stream");
reqEntity.setChunked(true); // Send in multiple parts if needed
httppost.setEntity(reqEntity);
//HttpResponse response = httpclient.execute(httppost);
//Do something with response...
} catch (Exception e) {
// show error
}