Android file uploading with Apache HTTPClient library -
i have problems import libraries in "libs" folder of android project. tried jersey client , apache http client libraries , both doesn't works.
i have configured manifest file this:
<?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>
calling client in main activity:
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(); }
and restclient class using apache frameworks:
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"); } }
and log after launching:
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 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 start activity componentinfo{com.example.androidclient/com.example.androidclient.mainactivity}: android.os.networkonmainthreadexception 07-24 06:45:25.084: e/androidruntime(3856): @ android.app.activitythread.performlaunchactivity(activitythread.java:2180) 07-24 06:45:25.084: e/androidruntime(3856): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 07-24 06:45:25.084: e/androidruntime(3856): @ android.app.activitythread.access$600(activitythread.java:141) 07-24 06:45:25.084: e/androidruntime(3856): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 07-24 06:45:25.084: e/androidruntime(3856): @ android.os.handler.dispatchmessage(handler.java:99) 07-24 06:45:25.084: e/androidruntime(3856): @ android.os.looper.loop(looper.java:137) 07-24 06:45:25.084: e/androidruntime(3856): @ android.app.activitythread.main(activitythread.java:5041) 07-24 06:45:25.084: e/androidruntime(3856): @ java.lang.reflect.method.invokenative(native method) 07-24 06:45:25.084: e/androidruntime(3856): @ java.lang.reflect.method.invoke(method.java:511) 07-24 06:45:25.084: e/androidruntime(3856): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 07-24 06:45:25.084: e/androidruntime(3856): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 07-24 06:45:25.084: e/androidruntime(3856): @ 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): @ android.os.strictmode$androidblockguardpolicy.onnetwork(strictmode.java:1117) 07-24 06:45:25.084: e/androidruntime(3856): @ java.net.inetaddress.lookuphostbyname(inetaddress.java:385) 07-24 06:45:25.084: e/androidruntime(3856): @ java.net.inetaddress.getallbynameimpl(inetaddress.java:236) 07-24 06:45:25.084: e/androidruntime(3856): @ java.net.inetaddress.getallbyname(inetaddress.java:214) 07-24 06:45:25.084: e/androidruntime(3856): @ org.apache.http.impl.conn.defaultclientconnectionoperator.openconnection(defaultclientconnectionoperator.java:137) 07-24 06:45:25.084: e/androidruntime(3856): @ org.apache.http.impl.conn.abstractpoolentry.open(abstractpoolentry.java:164) 07-24 06:45:25.084: e/androidruntime(3856): @ org.apache.http.impl.conn.abstractpooledconnadapter.open(abstractpooledconnadapter.java:119) 07-24 06:45:25.084: e/androidruntime(3856): @ org.apache.http.impl.client.defaultrequestdirector.execute(defaultrequestdirector.java:360) 07-24 06:45:25.084: e/androidruntime(3856): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:555) 07-24 06:45:25.084: e/androidruntime(3856): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:487) 07-24 06:45:25.084: e/androidruntime(3856): @ org.apache.http.impl.client.abstracthttpclient.execute(abstracthttpclient.java:465) 07-24 06:45:25.084: e/androidruntime(3856): @ com.example.androidclient.restclient.<init>(restclient.java:33) 07-24 06:45:25.084: e/androidruntime(3856): @ com.example.androidclient.mainactivity.oncreate(mainactivity.java:15) 07-24 06:45:25.084: e/androidruntime(3856): @ android.app.activity.performcreate(activity.java:5104) 07-24 06:45:25.084: e/androidruntime(3856): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 07-24 06:45:25.084: e/androidruntime(3856): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) 07-24 06:45:25.084: e/androidruntime(3856): ... 11 more
in libs there are:
android-support-v4.jar httpclient-4.2.5.jar httpcore-4.2.4.jar httpmime-4.2.5.jar
anyone met kind of problems?
Comments
Post a Comment