我使用android将图像从我的android模拟器发送到服务器(我的本地机器)
MainActivity.java
public class MainActivity extends Activity {
Button submit;
ProgressDialog pDialog;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit = (Button) findViewById(R.id.SUBMIT_BUTTON_ID);
imageView = (ImageView) findViewById(R.id.imageView1);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new MainTest().execute();
}
});
}
/**
* Method to post the image to the server.
* U will have to change the url which will accept the image data.
* @throws IOException
*/
public void postImageData() throws IOException
{
Drawable myDrawable = getResources().getDrawable(R.drawable.image);
Bitmap bitmap = ((BitmapDrawable)myDrawable).getBitmap();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://127.0.0.1/Details/");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
try{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
reqEntity.addPart("key", bab);
}
catch(Exception e){
//Log.v("Exception in Image", ""+e);
reqEntity.addPart("key", new StringBody(""));
}
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
}
public class MainTest extends AsyncTask<String, Integer, String> {
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Loading..");
pDialog.setIndeterminate(true);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
try {
postImageData();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// data=jobj.toString();
pDialog.dismiss();
}
}
}
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="135dp"
android:layout_height="181dp"
android:layout_alignParentTop="true"
android:layout_marginTop="32dp"
android:clickable="false"
android:src="@drawable/image" />
<Button
android:id="@+id/SUBMIT_BUTTON_ID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="47dp"
android:text="SUBMIT" />
</LinearLayout>
日志
12-07 17:42:21.560: E/AndroidRuntime(345): FATAL EXCEPTION: AsyncTask #1
12-07 17:42:21.560: E/AndroidRuntime(345): java.lang.RuntimeException: An error occured while executing doInBackground()
12-07 17:42:21.560: E/AndroidRuntime(345): at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-07 17:42:21.560: E/AndroidRuntime(345): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-07 17:42:21.560: E/AndroidRuntime(345): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-07 17:42:21.560: E/AndroidRuntime(345): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-07 17:42:21.560: E/AndroidRuntime(345): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-07 17:42:21.560: E/AndroidRuntime(345): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
12-07 17:42:21.560: E/AndroidRuntime(345): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
12-07 17:42:21.560: E/AndroidRuntime(345): at java.lang.Thread.run(Thread.java:1096)
12-07 17:42:21.560: E/AndroidRuntime(345): Caused by: java.lang.NoClassDefFoundError: org.apache.http.entity.mime.content.ByteArrayBody
12-07 17:42:21.560: E/AndroidRuntime(345): at com.example.datapostingproject.MainActivity.postImageData(MainActivity.java:97)
12-07 17:42:21.560: E/AndroidRuntime(345): at com.example.datapostingproject.MainActivity$MainTest.doInBackground(MainActivity.java:129)
12-07 17:42:21.560: E/AndroidRuntime(345): at com.example.datapostingproject.MainActivity$MainTest.doInBackground(MainActivity.java:1)
12-07 17:42:21.560: E/AndroidRuntime(345): at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-07 17:42:21.560: E/AndroidRuntime(345): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-07 17:42:21.560: E/AndroidRuntime(345): ... 4 more
12-07 17:42:23.700: E/WindowManager(345): Activity com.example.datapostingproject.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@45f39ba0 that was originally added here
12-07 17:42:23.700: E/WindowManager(345): android.view.WindowLeaked: Activity com.example.datapostingproject.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@45f39ba0 that was originally added here
12-07 17:42:23.700: E/WindowManager(345): at android.view.ViewRoot.<init>(ViewRoot.java:247)
12-07 17:42:23.700: E/WindowManager(345): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
12-07 17:42:23.700: E/WindowManager(345): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
12-07 17:42:23.700: E/WindowManager(345): at android.view.Window$LocalWindowManager.addView(Window.java:424)
12-07 17:42:23.700: E/WindowManager(345): at android.app.Dialog.show(Dialog.java:241)
12-07 17:42:23.700: E/WindowManager(345): at com.example.datapostingproject.MainActivity$MainTest.onPreExecute(MainActivity.java:122)
12-07 17:42:23.700: E/WindowManager(345): at android.os.AsyncTask.execute(AsyncTask.java:391)
12-07 17:42:23.700: E/WindowManager(345): at com.example.datapostingproject.MainActivity$1.onClick(MainActivity.java:70)
12-07 17:42:23.700: E/WindowManager(345): at android.view.View.performClick(View.java:2408)
12-07 17:42:23.700: E/WindowManager(345): at android.view.View$PerformClick.run(View.java:8816)
12-07 17:42:23.700: E/WindowManager(345): at android.os.Handler.handleCallback(Handler.java:587)
12-07 17:42:23.700: E/WindowManager(345): at android.os.Handler.dispatchMessage(Handler.java:92)
12-07 17:42:23.700: E/WindowManager(345): at android.os.Looper.loop(Looper.java:123)
12-07 17:42:23.700: E/WindowManager(345): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-07 17:42:23.700: E/WindowManager(345): at java.lang.reflect.Method.invokeNative(Native Method)
12-07 17:42:23.700: E/WindowManager(345): at java.lang.reflect.Method.invoke(Method.java:521)
12-07 17:42:23.700: E/WindowManager(345): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-07 17:42:23.700: E/WindowManager(345): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-07 17:42:23.700: E/WindowManager(345): at dalvik.system.NativeStart.main(Native Method)
如何解决这个问题?
感谢
答案 0 :(得分:0)
为了正确包含外部jar(如Apache HttpMime),请阅读: http://mobile.tutsplus.com/tutorials/android/jar-for-android/