2016-12-13 75 views
0

我有這個問題,讓我想,我使用OkHTTp連接到我的WebAPi服務。問題是我不斷收到「空」結果在android端。下面是Android端代碼:在Android中使用OKHTTP從ASP.NET WebApi請求GET服務

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

    OkHttpClient client = new OkHttpClient(); 
    String URL = "http://localhost:8306/api/uconnectservice"; 

    String JsonStringObject; 

    public OkHttpHandlerIncoming() { 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    protected String doInBackground(String... params) { 



     Request request = new Request.Builder().url(params[0]).build(); 
     // Request request = new 
     // Request.Builder().url(params[0]).post(formBody).build(); 
     try { 
      Response response = client.newCall(request).execute(); 
      Log.e("Execute Request", request.toString()); 
      if (!response.isSuccessful()) 
       throw new IOException("Unexpected code " + response.toString()); 
      return response.body().string(); 

     } catch (Exception e) { 
     } 

     return null; 
    } 

} 

我可以把從主線程調用如下

public void DATAFROMSERVICE(){ 
     StringBuilder outputText = new StringBuilder("Result:"); 

     OkHttpHandlerIncoming client = new OkHttpHandlerIncoming(); 

     String result = null; 
     try { 
      result = client.execute().get(); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ExecutionException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     Log.e("From Service", outputText.append(result + "\n").toString()); 

    } 
//////////////////////////////////////// 
Also here is the code from the server side 

    public class UConnectServiceController : ApiController 
    { 
     private IUconnectRepository _UconnectHandler; 

     public UConnectServiceController() 
     { 
      _UconnectHandler = new UconnectRepository(); 
     } 

     public UConnectServiceController(IUconnectRepository repository) 
     { 
      if (_UconnectHandler == null) 
      { 
       throw new ArgumentNullException("repository"); 
      } 
      _UconnectHandler = repository; 
     } 

     /////I am calling this service-> GET api/uconnectservice 
     public List<AcountTypesRegistered> Get() 
     { 
      return _UconnectHandler.GetAll(); 
     } 

代碼繼續...

請能有人指導我什麼錯誤或任何更好的建議 thnks

這裏是我的日誌貓:

12-13 03:20:54.360: W/System.err(26811): at java.util.concurrent.FutureTask.report(FutureTask.java:93) 
12-13 03:20:54.360: W/System.err(26811): at java.util.concurrent.FutureTask.get(FutureTask.java:163) 
12-13 03:20:54.360: W/System.err(26811): at android.os.AsyncTask.get(AsyncTask.java:483) 
12-13 03:20:54.360: W/System.err(26811): at com.nickSoft.unics_alpha.accountdetails.DATAFROMSERVICE(accountdetails.java:198) 
12-13 03:20:54.360: W/System.err(26811): at com.nickSoft.unics_alpha.accountdetails.onCreate(accountdetails.java:120) 
12-13 03:20:54.360: W/System.err(26811): at android.app.Activity.performCreate(Activity.java:5231) 
12-13 03:20:54.360: W/System.err(26811): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
12-13 03:20:54.360: W/System.err(26811): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210) 
12-13 03:20:54.360: W/System.err(26811): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296) 
12-13 03:20:54.360: W/System.err(26811): at android.app.ActivityThread.access$800(ActivityThread.java:145) 
12-13 03:20:54.360: W/System.err(26811): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243) 
12-13 03:20:54.360: W/System.err(26811): at android.os.Handler.dispatchMessage(Handler.java:102) 
12-13 03:20:54.360: W/System.err(26811): at android.os.Looper.loop(Looper.java:136) 
12-13 03:20:54.360: W/System.err(26811): at android.app.ActivityThread.main(ActivityThread.java:5136) 
12-13 03:20:54.360: W/System.err(26811): at java.lang.reflect.Method.invokeNative(Native Method) 
12-13 03:20:54.360: W/System.err(26811): at java.lang.reflect.Method.invoke(Method.java:515) 
12-13 03:20:54.360: W/System.err(26811): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:819) 
12-13 03:20:54.360: W/System.err(26811): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635) 
12-13 03:20:54.360: W/System.err(26811): at dalvik.system.NativeStart.main(Native Method) 
12-13 03:20:54.360: W/System.err(26811): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 
12-13 03:20:54.360: W/System.err(26811): at com.nickSoft.Connections.OkHttpHandlerIncoming.doInBackground(OkHttpHandlerIncoming.java:53) 
12-13 03:20:54.360: W/System.err(26811): at com.nickSoft.Connections.OkHttpHandlerIncoming.doInBackground(OkHttpHandlerIncoming.java:1) 
12-13 03:20:54.360: W/System.err(26811): at android.os.AsyncTask$2.call(AsyncTask.java:288) 
12-13 03:20:54.360: W/System.err(26811): at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
12-13 03:20:54.360: W/System.err(26811): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
12-13 03:20:54.360: W/System.err(26811): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
12-13 03:20:54.360: W/System.err(26811): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
12-13 03:20:54.360: W/System.err(26811): at java.lang.Thread.run(Thread.java:841) 
12-13 03:20:54.360: E/From Service(26811): Result:null 
12-13 03:20:54.360: W/dalvikvm(26811): threadid=11: thread exiting with uncaught exception (group=0x415ced88) 
12-13 03:20:54.360: E/test(26811): Exception 
12-13 03:20:54.370: E/AndroidRuntime(26811): FATAL EXCEPTION: AsyncTask #1 
12-13 03:20:54.370: E/AndroidRuntime(26811): Process: com.nickSoft.unics_alpha, PID: 26811 
12-13 03:20:54.370: E/AndroidRuntime(26811): java.lang.RuntimeException: An error occured while executing doInBackground() 
12-13 03:20:54.370: E/AndroidRuntime(26811): at android.os.AsyncTask$3.done(AsyncTask.java:300) 
12-13 03:20:54.370: E/AndroidRuntime(26811): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 
12-13 03:20:54.370: E/AndroidRuntime(26811): at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 
12-13 03:20:54.370: E/AndroidRuntime(26811): at java.util.concurrent.FutureTask.run(FutureTask.java:242) 
12-13 03:20:54.370: E/AndroidRuntime(26811): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
12-13 03:20:54.370: E/AndroidRuntime(26811): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
12-13 03:20:54.370: E/AndroidRuntime(26811): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
12-13 03:20:54.370: E/AndroidRuntime(26811): at java.lang.Thread.run(Thread.java:841) 
12-13 03:20:54.370: E/AndroidRuntime(26811): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 
12-13 03:20:54.370: E/AndroidRuntime(26811): at com.nickSoft.Connections.OkHttpHandlerIncoming.doInBackground(OkHttpHandlerIncoming.java:53) 
12-13 03:20:54.370: E/AndroidRuntime(26811): at com.nickSoft.Connections.OkHttpHandlerIncoming.doInBackground(OkHttpHandlerIncoming.java:1) 
12-13 03:20:54.370: E/AndroidRuntime(26811): at android.os.AsyncTask$2.call(AsyncTask.java:288) 
12-13 03:20:54.370: E/AndroidRuntime(26811): at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
12-13 03:20:54.370: E/AndroidRuntime(26811): ... 4 more 

///後的代碼編輯我得到這個logcat的////////////////

12-13 03:58:36.640: I/ActivityManager(28324): Timeline: Activity_idle id: [email protected] time:12169583 
12-13 03:59:30.560: W/System.err(29371): java.net.ConnectException: Failed to connect to localhost/127.0.0.1:8306 
12-13 03:59:30.560: W/System.err(29371): at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:189) 
12-13 03:59:30.560: W/System.err(29371): at okhttp3.internal.connection.RealConnection.buildConnection(RealConnection.java:173) 
12-13 03:59:30.560: W/System.err(29371): at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:114) 
12-13 03:59:30.560: W/System.err(29371): at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:193) 
12-13 03:59:30.560: W/System.err(29371): at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:129) 
12-13 03:59:30.560: W/System.err(29371): at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:98) 
12-13 03:59:30.570: W/System.err(29371): at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42) 
12-13 03:59:30.570: W/System.err(29371): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
12-13 03:59:30.570: W/System.err(29371): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
12-13 03:59:30.570: W/System.err(29371): at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:109) 
12-13 03:59:30.570: W/System.err(29371): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
12-13 03:59:30.570: W/System.err(29371): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
12-13 03:59:30.570: W/System.err(29371): at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) 
12-13 03:59:30.570: W/System.err(29371): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
12-13 03:59:30.570: W/System.err(29371): at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:124) 
12-13 03:59:30.570: W/System.err(29371): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
12-13 03:59:30.570: W/System.err(29371): at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
12-13 03:59:30.570: W/System.err(29371): at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:170) 
12-13 03:59:30.570: W/System.err(29371): at okhttp3.RealCall.access$100(RealCall.java:33) 
12-13 03:59:30.570: W/System.err(29371): at okhttp3.RealCall$AsyncCall.execute(RealCall.java:120) 
12-13 03:59:30.570: W/System.err(29371): at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 
12-13 03:59:30.570: W/System.err(29371): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
12-13 03:59:30.570: W/System.err(29371): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
12-13 03:59:30.570: W/System.err(29371): at java.lang.Thread.run(Thread.java:841) 
+0

我強烈建議檢出https://square.github.io/retrofit/。它在默認情況下使用OkHTTP,但提供了與api的非常簡單的交互。 – Orbit

+0

thnx @ Orbit,調查它 – user3015410

+0

你正在無聲無息地吞下所有的異常,用'catch(Exception e){}'你應該在裏面添加'e.printStackTrace();'來幫助你弄清楚你的調用是怎麼回事 – iagreen

回答

0

你的錯誤是從該行未來 -

Request request = new Request.Builder().url(params[0]).build(); 

,因爲你不是傳遞一個StringAsyncTask這裏 -

result = client.execute().get(); 

根據你的代碼,我猜你R鍵通URL在那裏 -

result = client.execute().get(URL); 

OkHttp可以在後臺執行的命令。如果不是在AsyncTask中調用execute,而是通過asynchronous call使用enqueue讓OkHttp爲您處理後臺任務,那麼您的代碼會簡單得多。

+0

好吧@iagreen看着它,thnxs爲您的輸入。 – user3015410

+0

確定@iagreen,但我得到另一個logcat說我無法連接(請參閱問題編輯) – user3015410

+0

嗯,看起來像你的服務器沒有接收連接。請檢查以確保您的服務器在您的URL中指定的地址和端口上運行。你可能需要用你的asp計算機的IP地址來替換'localhost',因爲在這個例子中'localhost'指的是android設備,你不太可能在你的手機上運行asp! – iagreen