我在Android项目的“libs”文件夹中导入库时遇到问题。我尝试使用Jersey客户端和Apache HTTP客户端库,两者都不起作用。
我已经配置了我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidclient"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.androidclient.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
在主要活动中呼叫客户:
package com.example.androidclient;
import android.os.Bundle;
import android.os.NetworkOnMainThreadException;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RestClient client = new RestClient();
}
和RestClient类相比,使用apache框架:
package com.example.androidclient;
import java.io.File;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
public class RestClient {
public RestClient() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://localhost:8080/RESTfulImg/rest/file/upload");
File file= new File("http://www.mkyong.com/wp-content/uploads/2009/01/java-project.png");
ContentBody fb = new FileBody(file, "image/jpeg");
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.STRICT);
entity.addPart("file", fb);
httppost.setEntity(entity);
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("file "+response.toString()+""+" e stato inviato al server");
}
}
启动后我的日志说什么:
07-24 06:45:24.282: E/Trace(3856): error opening trace file: No such file or directory (2)
07-24 06:45:24.992: D/AndroidRuntime(3856): Shutting down VM
07-24 06:45:25.042: W/dalvikvm(3856): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-24 06:45:25.084: E/AndroidRuntime(3856): FATAL EXCEPTION: main
07-24 06:45:25.084: E/AndroidRuntime(3856): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidclient/com.example.androidclient.MainActivity}: android.os.NetworkOnMainThreadException
07-24 06:45:25.084: E/AndroidRuntime(3856): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-24 06:45:25.084: E/AndroidRuntime(3856): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-24 06:45:25.084: E/AndroidRuntime(3856): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-24 06:45:25.084: E/AndroidRuntime(3856): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-24 06:45:25.084: E/AndroidRuntime(3856): at android.os.Handler.dispatchMessage(Handler.java:99)
07-24 06:45:25.084: E/AndroidRuntime(3856): at android.os.Looper.loop(Looper.java:137)
07-24 06:45:25.084: E/AndroidRuntime(3856): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-24 06:45:25.084: E/AndroidRuntime(3856): at java.lang.reflect.Method.invokeNative(Native Method)
07-24 06:45:25.084: E/AndroidRuntime(3856): at java.lang.reflect.Method.invoke(Method.java:511)
07-24 06:45:25.084: E/AndroidRuntime(3856): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-24 06:45:25.084: E/AndroidRuntime(3856): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-24 06:45:25.084: E/AndroidRuntime(3856): at dalvik.system.NativeStart.main(Native Method)
07-24 06:45:25.084: E/AndroidRuntime(3856): Caused by: android.os.NetworkOnMainThreadException
07-24 06:45:25.084: E/AndroidRuntime(3856): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
07-24 06:45:25.084: E/AndroidRuntime(3856): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
07-24 06:45:25.084: E/AndroidRuntime(3856): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
07-24 06:45:25.084: E/AndroidRuntime(3856): at java.net.InetAddress.getAllByName(InetAddress.java:214)
07-24 06:45:25.084: E/AndroidRuntime(3856): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
07-24 06:45:25.084: E/AndroidRuntime(3856): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-24 06:45:25.084: E/AndroidRuntime(3856): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-24 06:45:25.084: E/AndroidRuntime(3856): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-24 06:45:25.084: E/AndroidRuntime(3856): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-24 06:45:25.084: E/AndroidRuntime(3856): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-24 06:45:25.084: E/AndroidRuntime(3856): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-24 06:45:25.084: E/AndroidRuntime(3856): at com.example.androidclient.RestClient.<init>(RestClient.java:33)
07-24 06:45:25.084: E/AndroidRuntime(3856): at com.example.androidclient.MainActivity.onCreate(MainActivity.java:15)
07-24 06:45:25.084: E/AndroidRuntime(3856): at android.app.Activity.performCreate(Activity.java:5104)
07-24 06:45:25.084: E/AndroidRuntime(3856): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-24 06:45:25.084: E/AndroidRuntime(3856): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-24 06:45:25.084: E/AndroidRuntime(3856): ... 11 more
在我的库中有:
机器人支撑-v4.jar HttpClient的-4.2.5.jar 的HttpCore-4.2.4.jar httpmime-4.2.5.jar
有人遇到过这种问题吗?