我想一個單一的形象在Android簡單的圖片上傳到從Android模擬器
- 張貼到我的本地服務器簡單的本地服務器我已經在這裏,這幾天前試着和我以前的 Stackoverflow post here
- 我能得到完成
- 今天的工作時,我再次重試此我得到的錯誤
MainActivity.java
public class MainActivity extends Activity {
Button submit;
ProgressDialog pDialog;
InputStream is;
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() {
try
{
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://10.0.2.2:7002/Details/");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
try{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmapOrg.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("picture", 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);
}
}catch(Exception e){
e.getStackTrace();
}
}
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) {
postImageData();
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
// data=jobj.toString();
pDialog.dismiss();
}
}
}
登錄 ::
12-12 13:55:30.430: E/dalvikvm(541): Could not find class 'org.apache.http.entity.mime.MultipartEntity', referenced from method com.example.datapostingproject.MainActivity.postImageData
12-12 13:55:30.430: W/dalvikvm(541): VFY: unable to resolve new-instance 747 (Lorg/apache/http/entity/mime/MultipartEntity;) in Lcom/example/datapostingproject/MainActivity;
12-12 13:55:30.440: D/dalvikvm(541): VFY: replacing opcode 0x22 at 0x0016
12-12 13:55:30.440: D/dalvikvm(541): Making a copy of Lcom/example/datapostingproject/MainActivity;.postImageData code (297 bytes)
12-12 13:55:30.650: D/dalvikvm(541): GC freed 565 objects/50640 bytes in 87ms
12-12 13:55:30.871: D/dalvikvm(541): GC freed 45 objects/1728 bytes in 91ms
12-12 13:55:55.350: W/dalvikvm(541): threadid=15: thread exiting with uncaught exception (group=0x4001b188)
12-12 13:55:55.350: E/AndroidRuntime(541): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
12-12 13:55:55.850: E/AndroidRuntime(541): java.lang.RuntimeException: An error occured while executing doInBackground()
12-12 13:55:55.850: E/AndroidRuntime(541): at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-12 13:55:55.850: E/AndroidRuntime(541): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-12 13:55:55.850: E/AndroidRuntime(541): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-12 13:55:55.850: E/AndroidRuntime(541): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-12 13:55:55.850: E/AndroidRuntime(541): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-12 13:55:55.850: E/AndroidRuntime(541): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
12-12 13:55:55.850: E/AndroidRuntime(541): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
12-12 13:55:55.850: E/AndroidRuntime(541): at java.lang.Thread.run(Thread.java:1096)
12-12 13:55:55.850: E/AndroidRuntime(541): Caused by: java.lang.NoClassDefFoundError: org.apache.http.entity.mime.MultipartEntity
12-12 13:55:55.850: E/AndroidRuntime(541): at com.example.datapostingproject.MainActivity.postImageData(MainActivity.java:99)
12-12 13:55:55.850: E/AndroidRuntime(541): at com.example.datapostingproject.MainActivity$MainTest.doInBackground(MainActivity.java:154)
12-12 13:55:55.850: E/AndroidRuntime(541): at com.example.datapostingproject.MainActivity$MainTest.doInBackground(MainActivity.java:1)
12-12 13:55:55.850: E/AndroidRuntime(541): at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-12 13:55:55.850: E/AndroidRuntime(541): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-12 13:55:55.850: E/AndroidRuntime(541): ... 4 more
12-12 13:55:56.010: I/dalvikvm(541): threadid=7: reacting to signal 3
12-12 13:55:56.010: E/dalvikvm(541): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
注意 ::
我也有從這裏
使用的jar文件httpmime-4.2.3.jar ..... link
看來我必須按照步驟正確.....但什麼錯我在這裏做
True ..........其實我已經添加了Jar文件....右鍵單擊---添加jar --- ....但它沒有解決我的問題,然後複製jar文件手動到libs文件夾解決了問題...謝謝 – smriti3