Android 客户端将 byte[] 发送到 REST Web 服务时出错
这是我的第一篇文章;我是Android开发的新手,我编写了一个REST Web服务来接收字节[]:
@Path("scp")
public class ScpResources {
// ...
@POST
@Produces("text/plain")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public String upload(byte[] input) {
System.out.println("Input: " + new String(input));
return "File Sent!";
}
} // End
我用java独立应用程序(apache httpclient库)测试了这个ws,一切都很好,但是,当我尝试这样做时在 Android 应用程序中也是如此... :
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
@Override /** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v(TAG, "--------------> to WS");
try {
HttpClient client = new DefaultHttpClient();
String tmp = "abcdefghijklmnopqrstuvwxyz";
byte b[] = tmp.getBytes();
HttpPost httpPost = new HttpPost("http://192.168.0.116:8080/WSfEclipse/resources/scp");
ByteArrayEntity entity = new ByteArrayEntity(b);
entity.setContentType("application/octet-stream");
httpPost.setEntity(entity);
Log.e(TAG, ">Before error");
//if(client == null) Log.e(TAG, "HttpClient null");
//if(httpPost == null) Log.e(TAG, "HttpPost null");
client.execute(httpPost); // ERROR
} catch (Exception e) {
//if(e == null) Log.e(TAG, "Exception(e) null");
Log.e(TAG, e.getMessage());
}
} // End : onCreate
} // End : MainActivity
... logcat 打印:
ERROR/MainActivity(368): >Before error
AndroidRuntime(368): Shutting down VM
WARNING/dalvikvm(368): threadid=1: thread exiting with uncaught exception (group=0x40014760)
ERROR/AndroidRuntime(368): FATAL EXCEPTION: main
ERROR/AndroidRuntime(368): java.lang.RuntimeException: Unable to start activity ComponentInfo{.....
ERROR/AndroidRuntime(368): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
ERROR/AndroidRuntime(368): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
ERROR/AndroidRuntime(368): at android.app.ActivityThread.access$500(ActivityThread.java:122)
ERROR/AndroidRuntime(368): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
ERROR/AndroidRuntime(368): at android.os.Handler.dispatchMessage(Handler.java:99)
ERROR/AndroidRuntime(368): at android.os.Looper.loop(Looper.java:132)
ERROR/AndroidRuntime(368): at android.app.ActivityThread.main(ActivityThread.java:3948)
ERROR/AndroidRuntime(368): at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(368): at java.lang.reflect.Method.invoke(Method.java:491)
ERROR/AndroidRuntime(368): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
ERROR/AndroidRuntime(368): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
ERROR/AndroidRuntime(368): at dalvik.system.NativeStart.main(Native Method)
ERROR/AndroidRuntime(368): Caused by: java.lang.NullPointerException: println needs a message
ERROR/AndroidRuntime(368): at android.util.Log.println_native(Native Method)
ERROR/AndroidRuntime(368): at android.util.Log.e(Log.java:230)
ERROR/AndroidRuntime(368): at MainActivity.onCreate(MainActivity.java:44)
ERROR/AndroidRuntime(368): at android.app.Activity.performCreate(Activity.java:4397)
ERROR/AndroidRuntime(368): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
ERROR/AndroidRuntime(368): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
ERROR/AndroidRuntime(368): ... 11 more
My AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.edu.radiogis.scpecg.clientecelular.activities"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="13" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@drawable/icon" android:label="ClienteSCP">
<activity android:name=".MainActivity"
android:label="ClienteSCP">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
当我删除 catch 代码时,没有出现错误;然而,应用程序不会将字节发送到 Web 服务。我检查了是否存在空引用(使用注释中的代码),但一切正常。我不知道发生了什么,也许我忘记了什么。
!!提前致谢!!
this is my first post; i'm a newbie on Android development, i wrote a REST web service for receiving a byte[]:
@Path("scp")
public class ScpResources {
// ...
@POST
@Produces("text/plain")
@Consumes(MediaType.APPLICATION_OCTET_STREAM)
public String upload(byte[] input) {
System.out.println("Input: " + new String(input));
return "File Sent!";
}
} // End
I tested this ws with a java stand alone application(apache httpclient libraries), and everything is ok, but, when i try to do the same thing in the Android application... :
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
@Override /** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v(TAG, "--------------> to WS");
try {
HttpClient client = new DefaultHttpClient();
String tmp = "abcdefghijklmnopqrstuvwxyz";
byte b[] = tmp.getBytes();
HttpPost httpPost = new HttpPost("http://192.168.0.116:8080/WSfEclipse/resources/scp");
ByteArrayEntity entity = new ByteArrayEntity(b);
entity.setContentType("application/octet-stream");
httpPost.setEntity(entity);
Log.e(TAG, ">Before error");
//if(client == null) Log.e(TAG, "HttpClient null");
//if(httpPost == null) Log.e(TAG, "HttpPost null");
client.execute(httpPost); // ERROR
} catch (Exception e) {
//if(e == null) Log.e(TAG, "Exception(e) null");
Log.e(TAG, e.getMessage());
}
} // End : onCreate
} // End : MainActivity
... the logcat prints:
ERROR/MainActivity(368): >Before error
AndroidRuntime(368): Shutting down VM
WARNING/dalvikvm(368): threadid=1: thread exiting with uncaught exception (group=0x40014760)
ERROR/AndroidRuntime(368): FATAL EXCEPTION: main
ERROR/AndroidRuntime(368): java.lang.RuntimeException: Unable to start activity ComponentInfo{.....
ERROR/AndroidRuntime(368): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
ERROR/AndroidRuntime(368): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
ERROR/AndroidRuntime(368): at android.app.ActivityThread.access$500(ActivityThread.java:122)
ERROR/AndroidRuntime(368): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
ERROR/AndroidRuntime(368): at android.os.Handler.dispatchMessage(Handler.java:99)
ERROR/AndroidRuntime(368): at android.os.Looper.loop(Looper.java:132)
ERROR/AndroidRuntime(368): at android.app.ActivityThread.main(ActivityThread.java:3948)
ERROR/AndroidRuntime(368): at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(368): at java.lang.reflect.Method.invoke(Method.java:491)
ERROR/AndroidRuntime(368): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
ERROR/AndroidRuntime(368): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
ERROR/AndroidRuntime(368): at dalvik.system.NativeStart.main(Native Method)
ERROR/AndroidRuntime(368): Caused by: java.lang.NullPointerException: println needs a message
ERROR/AndroidRuntime(368): at android.util.Log.println_native(Native Method)
ERROR/AndroidRuntime(368): at android.util.Log.e(Log.java:230)
ERROR/AndroidRuntime(368): at MainActivity.onCreate(MainActivity.java:44)
ERROR/AndroidRuntime(368): at android.app.Activity.performCreate(Activity.java:4397)
ERROR/AndroidRuntime(368): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
ERROR/AndroidRuntime(368): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
ERROR/AndroidRuntime(368): ... 11 more
My AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.edu.radiogis.scpecg.clientecelular.activities"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="13" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@drawable/icon" android:label="ClienteSCP">
<activity android:name=".MainActivity"
android:label="ClienteSCP">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
When i removed the catch code, no errors appear; nevertheless, the application doesn't send the bytes to the web service. I checked if there were null references(Using the code in the comments), but everything was ok. I don't know what is happening, maybe i'm forgetting something.
!!Thanks in advance!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您看到的是 Log.e 方法引发的异常,因为您传递给它的消息文本 (e.getMessage) 为 null。
您应该检查从 http 代码中获得的异常类型。将您的日志记录语句替换为 e.printStackTrace()。
What you see is an exception raised by Log.e method, because the message text you pass to it (e.getMessage) is null.
You should check what kind of exception you get from your http code. Replace your logging statement with e.printStackTrace().
我找到了解决方案,包没问题(抱歉我没有显示包声明),我使用了 e.printStackTrace(),它显示 Android NetworkOnMainThreadException,所以我将代码移至 AsyncTask:
和!!Jackpot!! ,成功了,非常感谢。
i found the solution, the package was ok(sorry i didn't show the package statement), i used e.printStackTrace(), and it shows Android NetworkOnMainThreadException, so i moved the code to an AsyncTask:
And !!Jackpot!!, success, thank you so much.
基于异常和代码中缺少包定义,我认为您忘记将代码放入清单中列出的包中,特别是:
com.edu.radiogis.scpecg。 clientecellular.activities
Based on the exception and the lack of a package definition in your code, I am thinking that you have forgotten to place your code in the package that is listed in the Manifest, specifically this:
com.edu.radiogis.scpecg.clientecelular.activities