我尝试从android app发送pdf和文本到服务器,我成功发送了文本。问题是我无法发送pdf,我不知道如果android代码或php代码中的问题可以帮助我解决这个问题 android代码
private class postData extends AsyncTask<String, Void, String> {
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) {
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
File uploadFile1 = new File("/sdcard/Download/Testing foundation.pdf");
FileBody fileBody = new FileBody( uploadFile1 , ContentType.DEFAULT_BINARY);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addPart("cv",new FileBody(uploadFile1));
builder.addPart("email", new StringBody(eemail));
builder.addPart("phone", new StringBody(pphone));
builder.addPart("firstname", new StringBody(firstname));
builder.addPart("lastname", new StringBody(lastname));
HttpEntity entity = builder.build();
httppost.setEntity(entity);
try {
httpclient.execute(httppost);
// filetest();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
responseStr = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
// TODO Auto-generated catch block
Log.i("HTTP Failed", e.toString());
}
return responseStr;
}
protected void onPostExecute(String responseStr) {
super.onPostExecute(responseStr);
String result="";
String test="";
String msg="msg";
int x=1;
Toast.makeText(getActivity(),responseStr,Toast.LENGTH_LONG).show();
try {
JSONArray pp=new JSONArray(responseStr);
result = pp.getString(x) Toast.makeText(getActivity(),result,Toast.LENGTH_LONG).show();
//result is key for which you need to retrieve data
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getActivity(),e.toString(),Toast.LENGTH_LONG).show();
}
}
php代码
public function add_resume(Request $request){
$resume = new Resume;
$resume->first_name = $request->firstname;
$resume->last_name = $request->lastname;
$resume->phone = $request->phone;
$resume->email = $request->email;
$file = $request->file('cv');
$ext = $file->guessExtension();
$ext=".".$ext;
$filename=md5(time().mt_rand());
$file->move(storage_path('app/public/uploads'),$filename.$ext);
$resume->cv = $filename.$ext;
$resume->save();
return response()->json(['cv','1']);
}
答案 0 :(得分:0)
public void onBindViewHolder(final MyAttachmentViewHolder holder, final int position) {
attachments=alist.get(position);
holder.filename.setText(attachments.get(FragmentAssignmentDetails.FILE));
holder.viewButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean internet = new Internetcheck(contex).isOnline();
if (internet) {
attachments=alist.get(position);
String filename = attachments.get(FragmentAssignmentDetails.FILE);
Bundle bundle = new Bundle();
filename.toLowerCase();
if (filename.endsWith("jpeg") || filename.endsWith("jpg") || filename.endsWith("png") || filename.endsWith("JPG") || filename.endsWith("PNG") || filename.endsWith("JPEG")) {
bundle.putString("filetype", "imagefile");
} else if (filename.endsWith("flv") || filename.endsWith("mp4") || filename.endsWith("mov")) {
bundle.putString("filetype", "videofile");
} else {
bundle.putString("filetype", "noimage");
}
Fragment fragment = new FragmentWebview();
bundle.putString("link", (url+filename));
fragment.setArguments(bundle);
android.support.v4.app.FragmentTransaction fragmentTransaction = ((android.support.v4.app.FragmentActivity) contex).getSupportFragmentManager().beginTransaction();
fragmentTransaction.setTransition(android.support.v4.app.FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
} else {
Toast.makeText(contex, "No internet", Toast.LENGTH_SHORT).show();
}
}
}
);
holder.downloadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
attachments=alist.get(position);
filename = attachments.get(FragmentAssignmentDetails.FILE);
String filelink = url+filename;
sdCard = Environment.getExternalStorageDirectory();
directory = new File(sdCard.getAbsolutePath() + "/" + contex.getString(R.string.sdirname) + "/" + foldername + "/" + filename);
String str = directory.getAbsolutePath().toString();
if (directory.exists()) {
Intent intent = new Intent(Intent.ACTION_VIEW);
if (str.endsWith("pdf") || str.endsWith("PDF")) {
intent.setDataAndType(Uri.fromFile(directory), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
contex.startActivity(intent);
} else if (str.endsWith("docx") || str.endsWith("doc")) {
intent.setDataAndType(Uri.fromFile(directory), "application/msword");
contex.startActivity(intent);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
} else if (str.endsWith("jpeg") || str.endsWith("jpg") || str.endsWith("png") || str.endsWith("JPG") || str.endsWith("PNG") || str.endsWith("JPEG")) {
intent.setDataAndType(Uri.fromFile(directory), "image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
contex.startActivity(intent);
}
else{
Toast.makeText(contex, "App not found", Toast.LENGTH_SHORT).show();
}
}
else {
internet = new Internetcheck(contex).isOnline();
if (internet) {
task = (DownloadFileFromURL) new DownloadFileFromURL(contex).execute(filelink);
}
else {
Toast.makeText(contex, "No internet", Toast.LENGTH_SHORT).show();
}
}
}
});
}