gpt4 book ai didi

java - 在 Android 中使用 ion 库将文件(多部分/表单数据)上传到服务器失败

转载 作者:行者123 更新时间:2023-12-02 13:16:20 24 4
gpt4 key购买 nike

我是 android 新手,正在尝试使用 ion 库将文件上传到服务器 ion library但文件崩溃了。出现此错误。

FATAL EXCEPTION: Thread-55725
Process: transitions.com.example.huzy_kamz.interview, PID: 4635
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:209)
at android.os.Handler.<init>(Handler.java:123)
at android.app.Dialog.<init>(Dialog.java:122)
at android.app.AlertDialog.<init>(AlertDialog.java:200)
at android.app.AlertDialog.<init>(AlertDialog.java:196)
at android.app.AlertDialog.<init>(AlertDialog.java:141)
at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
at transitions.com.example.huzy_kamz.interview.MainActivity.uploadFile(MainActivity.java:131)
at transitions.com.example.huzy_kamz.interview.MainActivity$2$1.run(MainActivity.java:66)
at java.lang.Thread.run(Thread.java:818)

这就是我使用 ion 库上传文件的方式

public class MainActivity extends AppCompatActivity {
private static final int PICK_FILE_REQUEST = 1;
private static final String TAG = MainActivity.class.getSimpleName();
private String selectedFilePath;
private String SERVER_URL = "http://192.168.43.104:8093/PoliceApp/FileUpload.aspx";
ImageView ivAttachment;
private String UPLOAD_URL ="http://192.168.43.104:8093/PoliceApp/ImageUpload.aspx";
ProgressDialog dialog;
private ImageView imageView;
private EditText firstname_edt,lastname_edt,email_edt,jobtitle_edt,source_edt;
private TextView directory_txt;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView= (ImageView)findViewById(R.id.imageView);
firstname_edt=(EditText)findViewById(R.id.firstname_edt);
lastname_edt=(EditText)findViewById(R.id.lastname_edt);
email_edt=(EditText)findViewById(R.id.email_edt);
jobtitle_edt=(EditText)findViewById(R.id.jobtitle_edt);
source_edt=(EditText)findViewById(R.id.source_edt);
button=(Button)findViewById(R.id.button);
directory_txt=(TextView)findViewById(R.id.directory_txt);

imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showFileChooser();
}
});

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(selectedFilePath != null){
dialog = ProgressDialog.show(MainActivity.this,"","Uploading File...",true);

new Thread(new Runnable() {
@Override
public void run() {
//creating new thread to handle Http Operations


uploadFile(selectedFilePath);
// OnSendFileInfo();

}
}).start();
}else{
Toast.makeText(MainActivity.this,"Please choose a File First",Toast.LENGTH_SHORT).show();
}
}
});






}


// Gallery pick

private void showFileChooser() {
Intent intent = new Intent();
//sets the select file to all types of files
intent.setType("*/*");
//allows to select data and return it
intent.setAction(Intent.ACTION_GET_CONTENT);
//starts new activity to select file and return data
startActivityForResult(Intent.createChooser(intent,"Choose File to Upload.."),PICK_FILE_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK){
if(requestCode == PICK_FILE_REQUEST){
if(data == null){
//no data present
return;
}


Uri selectedFileUri = data.getData();
selectedFilePath = FilePath.getPath(this,selectedFileUri);
Log.i(TAG,"Selected File Path:" + selectedFilePath);

if(selectedFilePath != null && !selectedFilePath.equals("")){
directory_txt.setText(selectedFilePath);
}else{
Toast.makeText(this,"Cannot upload file to server",Toast.LENGTH_SHORT).show();
}
}
}
}





public void uploadFile(final String selectedFilePath){




final ProgressDialog pd;
pd = new ProgressDialog(MainActivity.this);
pd.setMessage("Uploading File...");
pd.setCancelable(false);
pd.show();
final String url_ = "";

Ion.with(MainActivity.this)
.load(SERVER_URL)
.uploadProgressDialog(pd)
.setMultipartParameter("uploaded_file",selectedFilePath)
.setMultipartFile("archive", "application/zip", new File(selectedFilePath))
.asString()
.setCallback(new FutureCallback<String>() {
@Override
public void onCompleted(Exception e, String result) {




Toast.makeText(MainActivity.this," "+result,Toast.LENGTH_LONG).show();

pd.dismiss();



}
});


}
}

我的方法包含 ionic 库上传 PDF 文件。

 public void uploadFile(final String selectedFilePath){




final ProgressDialog pd;
pd = new ProgressDialog(MainActivity.this);
pd.setMessage("Uploading ...");
pd.setCancelable(false);
pd.show();
final String url_ = "";
//File file = new File(this.getFilesDir().getAbsolutePath() + "/thesubdirectory/the.zip");
File f = new File(getApplicationContext().getFilesDir().getAbsolutePath()+selectedFilePath);


try {
RandomAccessFile rf = new RandomAccessFile(f, "rw");
rf.setLength(1024 * 1024 * 2);
} catch (Exception e) {
System.err.println(e);
}
File echoedFile = getFileStreamPath("echo");
Ion.with(MainActivity.this)
.load(SERVER_URL)
.uploadProgressDialog(pd)
.setMultipartFile("uploaded_file",f)
.write(echoedFile)
.setCallback(new FutureCallback<File>() {
@Override
public void onCompleted(Exception e, File result) {


try {

Toast.makeText(MainActivity.this, " " + result, Toast.LENGTH_LONG).show();

System.out.println("Error " + result);

pd.dismiss();

} catch (Exception e1) {
System.out.println("Error " + e1);
}
pd.dismiss();


}
});



}

我不知道哪里错了,我需要你的帮助,我哪里错了。我的后端工作正常,但问题出在上传过程中。

这是我的 C# Api,用于在指定文件夹中查找文件。

 public void FileSave(HttpPostedFile file)
{

try
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/FileStorage"), fileName);
file.SaveAs(path);
}

Response.Write("Upload successfull");

}
catch
{
Response.Write("An Error Ocurred");

}
}

最佳答案

从调用链中删除线程,因为 Ion 会为您管理该线程,并且您的异常与 Ion 无关(仅供引用)。

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(selectedFilePath != null){
dialog = ProgressDialog.show(MainActivity.this,"","Uploading File...",true);
uploadFile(selectedFilePath);
// OnSendFileInfo();
}else{
Toast.makeText(MainActivity.this,"Please choose a File First",Toast.LENGTH_SHORT).show();
}
}
});

您不能在非 Ui 线程中使用 View 。因此,正如我所看到的,您正在为线程调用的上传方法中创建一个进度对话框。该代码应该仅在主线程中。

关于java - 在 Android 中使用 ion 库将文件(多部分/表单数据)上传到服务器失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43752958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com