MultipartEntityBuilder 在 Http.Post.setEntity 上出错
Error on Http.Post.setEntity with MultipartEntityBuilder
当我遇到这个问题时,我试图将我的 Android 应用程序连接到本地主机上的 Flask 服务器。为了更容易检测到错误,我将向您提供我的 Android 应用程序的代码和 Android Studio 的日志输出。
首先,我的 Android 应用程序的代码:它只会在您触摸设备屏幕时尝试将图像上传到服务器。代码尚未完成,因为我在结束之前检测到错误。
public class Imagen extends ActionBarActivity {
private int SELECT_IMAGE = 237;
private int TAKE_PICTURE = 829;
private int n_touchs=0;
private EditText lblPhoto;
private ImageView imgPhoto;
private RelativeLayout world;
private HttpClient client=new DefaultHttpClient();
private HttpPost post=new HttpPost("192.168.1.138");
private MultipartEntityBuilder builder=MultipartEntityBuilder.create();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imagen);
lblPhoto=(EditText)findViewById(R.id.lblPhoto);
imgPhoto=(ImageView)findViewById(R.id.imgPhoto);
world=(RelativeLayout)findViewById(R.id.world);
world.setOnTouchListener(new RelativeLayout.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (n_touchs == 0) {
dialogPhoto();
n_touchs++;
}
return true;
}
});
}
private void dialogPhoto(){
try{
final CharSequence[] items = {"Seleccionar de la galería", "Hacer una foto"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Seleccionar una foto");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch(item){
case 0:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, SELECT_IMAGE);
break;
case 1:
startActivityForResult(new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE), TAKE_PICTURE);
break;
}
}
});
AlertDialog alert = builder.create();
alert.show();
} catch(Exception e){}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_imagen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try{
if (requestCode == SELECT_IMAGE) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
lblPhoto.setText("ok");
imgPhoto.setImageURI(selectedImage);
File file_image=new File(selectedImage.toString());
FileBody file_send=new FileBody(file_image);
builder.addPart("file",file_send);
HttpEntity entity=builder.build();
post.setEntity(entity);
}
}
if(requestCode == TAKE_PICTURE) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
lblPhoto.setText("ok");
imgPhoto.setImageURI(selectedImage);
File file_image=new File(selectedImage.toString());
FileBody file_send=new FileBody(file_image);
builder.addPart("file",file_send);
post.setEntity(builder.build());
}
}
} catch(Exception e){}
}
}
我的日志输出是这样的:
05-05 20:01:41.761: E/AndroidRuntime(11109): FATAL EXCEPTION: main
05-05 20:01:41.761: E/AndroidRuntime(11109): java.lang.NoSuchFieldError: org.apache.http.message.BasicHeaderValueFormatter.INSTANCE
05-05 20:01:41.761: E/AndroidRuntime(11109): at org.apache.http.entity.ContentType.toString(ContentType.java:153)
05-05 20:01:41.761: E/AndroidRuntime(11109): at org.apache.http.entity.mime.MultipartFormEntity.<init>(MultipartFormEntity.java:52)
05-05 20:01:41.761: E/AndroidRuntime(11109): at org.apache.http.entity.mime.MultipartEntityBuilder.buildEntity(MultipartEntityBuilder.java:226)
05-05 20:01:41.761: E/AndroidRuntime(11109): at org.apache.http.entity.mime.MultipartEntityBuilder.build(MultipartEntityBuilder.java:230)
05-05 20:01:41.761: E/AndroidRuntime(11109): at com.example.bryan.imagen.Imagen.onActivityResult(Imagen.java:122)
05-05 20:01:41.761: E/AndroidRuntime(11109): at android.app.Activity.dispatchActivityResult(Activity.java:5563)
05-05 20:01:41.761: E/AndroidRuntime(11109): at android.app.ActivityThread.deliverResults(ActivityThread.java:3514)
05-05 20:01:41.761: E/AndroidRuntime(11109): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3561)
05-05 20:01:41.761: E/AndroidRuntime(11109): at android.app.ActivityThread.access00(ActivityThread.java:168)
05-05 20:01:41.761: E/AndroidRuntime(11109): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1377)
05-05 20:01:41.761: E/AndroidRuntime(11109): at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 20:01:41.761: E/AndroidRuntime(11109): at android.os.Looper.loop(Looper.java:137)
05-05 20:01:41.761: E/AndroidRuntime(11109): at android.app.ActivityThread.main(ActivityThread.java:5493)
05-05 20:01:41.761: E/AndroidRuntime(11109): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 20:01:41.761: E/AndroidRuntime(11109): at java.lang.reflect.Method.invoke(Method.java:525)
05-05 20:01:41.761: E/AndroidRuntime(11109): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
05-05 20:01:41.761: E/AndroidRuntime(11109): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
05-05 20:01:41.761: E/AndroidRuntime(11109): at dalvik.system.NativeStart.main(Native Method)
我希望你能帮我解决这个错误,对不起我的英语。谢谢
当我看到这个 post 时,我正在阅读堆栈溢出中的 post :
我的问题有解决方案。
当我遇到这个问题时,我试图将我的 Android 应用程序连接到本地主机上的 Flask 服务器。为了更容易检测到错误,我将向您提供我的 Android 应用程序的代码和 Android Studio 的日志输出。
首先,我的 Android 应用程序的代码:它只会在您触摸设备屏幕时尝试将图像上传到服务器。代码尚未完成,因为我在结束之前检测到错误。
public class Imagen extends ActionBarActivity {
private int SELECT_IMAGE = 237;
private int TAKE_PICTURE = 829;
private int n_touchs=0;
private EditText lblPhoto;
private ImageView imgPhoto;
private RelativeLayout world;
private HttpClient client=new DefaultHttpClient();
private HttpPost post=new HttpPost("192.168.1.138");
private MultipartEntityBuilder builder=MultipartEntityBuilder.create();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imagen);
lblPhoto=(EditText)findViewById(R.id.lblPhoto);
imgPhoto=(ImageView)findViewById(R.id.imgPhoto);
world=(RelativeLayout)findViewById(R.id.world);
world.setOnTouchListener(new RelativeLayout.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (n_touchs == 0) {
dialogPhoto();
n_touchs++;
}
return true;
}
});
}
private void dialogPhoto(){
try{
final CharSequence[] items = {"Seleccionar de la galería", "Hacer una foto"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Seleccionar una foto");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch(item){
case 0:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, SELECT_IMAGE);
break;
case 1:
startActivityForResult(new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE), TAKE_PICTURE);
break;
}
}
});
AlertDialog alert = builder.create();
alert.show();
} catch(Exception e){}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_imagen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try{
if (requestCode == SELECT_IMAGE) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
lblPhoto.setText("ok");
imgPhoto.setImageURI(selectedImage);
File file_image=new File(selectedImage.toString());
FileBody file_send=new FileBody(file_image);
builder.addPart("file",file_send);
HttpEntity entity=builder.build();
post.setEntity(entity);
}
}
if(requestCode == TAKE_PICTURE) {
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
lblPhoto.setText("ok");
imgPhoto.setImageURI(selectedImage);
File file_image=new File(selectedImage.toString());
FileBody file_send=new FileBody(file_image);
builder.addPart("file",file_send);
post.setEntity(builder.build());
}
}
} catch(Exception e){}
}
}
我的日志输出是这样的:
05-05 20:01:41.761: E/AndroidRuntime(11109): FATAL EXCEPTION: main
05-05 20:01:41.761: E/AndroidRuntime(11109): java.lang.NoSuchFieldError: org.apache.http.message.BasicHeaderValueFormatter.INSTANCE
05-05 20:01:41.761: E/AndroidRuntime(11109): at org.apache.http.entity.ContentType.toString(ContentType.java:153)
05-05 20:01:41.761: E/AndroidRuntime(11109): at org.apache.http.entity.mime.MultipartFormEntity.<init>(MultipartFormEntity.java:52)
05-05 20:01:41.761: E/AndroidRuntime(11109): at org.apache.http.entity.mime.MultipartEntityBuilder.buildEntity(MultipartEntityBuilder.java:226)
05-05 20:01:41.761: E/AndroidRuntime(11109): at org.apache.http.entity.mime.MultipartEntityBuilder.build(MultipartEntityBuilder.java:230)
05-05 20:01:41.761: E/AndroidRuntime(11109): at com.example.bryan.imagen.Imagen.onActivityResult(Imagen.java:122)
05-05 20:01:41.761: E/AndroidRuntime(11109): at android.app.Activity.dispatchActivityResult(Activity.java:5563)
05-05 20:01:41.761: E/AndroidRuntime(11109): at android.app.ActivityThread.deliverResults(ActivityThread.java:3514)
05-05 20:01:41.761: E/AndroidRuntime(11109): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3561)
05-05 20:01:41.761: E/AndroidRuntime(11109): at android.app.ActivityThread.access00(ActivityThread.java:168)
05-05 20:01:41.761: E/AndroidRuntime(11109): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1377)
05-05 20:01:41.761: E/AndroidRuntime(11109): at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 20:01:41.761: E/AndroidRuntime(11109): at android.os.Looper.loop(Looper.java:137)
05-05 20:01:41.761: E/AndroidRuntime(11109): at android.app.ActivityThread.main(ActivityThread.java:5493)
05-05 20:01:41.761: E/AndroidRuntime(11109): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 20:01:41.761: E/AndroidRuntime(11109): at java.lang.reflect.Method.invoke(Method.java:525)
05-05 20:01:41.761: E/AndroidRuntime(11109): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
05-05 20:01:41.761: E/AndroidRuntime(11109): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
05-05 20:01:41.761: E/AndroidRuntime(11109): at dalvik.system.NativeStart.main(Native Method)
我希望你能帮我解决这个错误,对不起我的英语。谢谢
当我看到这个 post 时,我正在阅读堆栈溢出中的 post :
我的问题有解决方案。