我正在尝试从android
将单个图像发布到我的本地服务器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
似乎我已经按照正确的步骤进行了.....但我在这里做错了什么
答案 0 :(得分:1)
明确地引起:java.lang.NoClassDefFoundError: org.apache.http.entity.mime.MultipartEntity
在运行时,您无法使用此类,将Mime jar添加到lib forlder并添加到您的构建路径,这将解决此问题。
答案 1 :(得分:1)
首先删除该jar文件:
java.lang.NoClassDefFoundError:表示您没有正确地将jar文件添加到项目中。所以
导入该jar文件后:
右键点击 - &gt;属性 - &gt; Java构建路径 - &gt;添加外部Jar-&gt;申请 - &gt;确定
并将该jar文件添加到 libs 文件夹中。