2011-10-10 98 views
0

这是我的第一篇文章;我是在Android上开发一个新手,我写了一个REST Web服务接收一个byte []:发送字节[]到REST Web服务的Android客户端错误

@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 

我测试了此WS与Java独立的应用程序(Apache的HttpClient的库),并一切正常,但是,当我尝试做在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 

我的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服务。我检查是否有空引用(使用评论中的代码),但一切都很好。我不知道发生了什么,也许我忘记了一些事情。

!!提前致谢!

+0

请注意,您不应该在UI线程上执行潜在的长时间运行操作。它会阻止一切,你会得到臭名昭着的应用程序不响应对话框。结帐这看看如何做正确的方式:http://developer.android.com/resources/articles/painless-threading.html –

回答

0

基于异常和缺乏在你的代码包定义的,我以为你忘了把你的代码是清单中列出的包,特别是这样的:com.edu.radiogis.scpecg.clientecelular.activities

+0

MainActivity.onCreate执行: ... 在MainActivity.onCreate(MainActivity .java:44) ... – aav

+0

我也注意到了,但这一切似乎都很腥...... –

2

什么你请参阅Log.e方法引发的异常,因为您传递给它的消息文本(e.getMessage)为null。

您应该检查从您的http代码中获得哪种异常。用e.printStackTrace()替换你的日志语句。

2

我找到了解决办法,包是好的(对不起,我没有表现出包语句),我用e.printStackTrace(),它显示了Android的NetworkOnMainThreadException,让我感动的代码到的AsyncTask:

public class WSConnectionTask extends AsyncTask<Void, String, String> { 

HttpClient client; 
HttpPost httpPost; 
HttpResponse response; 

String tmp = "abcdefghijklmnopqrstuvwxyz"; 
byte[] b; 

@Override 
protected void onPreExecute() { 
    client = new DefaultHttpClient(); 
    b = tmp.getBytes(); 
} // End : onPreExecute 

@Override 
protected String doInBackground(Void... arg0) { 

    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); 

    try { 
     client.execute(httpPost); // Send request  
    } 
    catch (Exception e) {   
     e.printStackTrace(); 
    }  
    return "OK"; 

} // End : doInBackground 

} // End : WSConnectionTask 

和!!累积奖金,成功,非常感谢。

+0

接受你的答案,如果没有其他答案导致你这个解决方案。 Android标签中有很多开放问题(其中30,000个),请大家帮忙 – Merlin

相关问题