2012-04-09 111 views
0

我需要幫助..我試圖從我的android客戶端應用程序調用php webservices,並且我無法從主線程調用它,因爲SDK是15,並且我在網上搜索並我發現這個問題的解決方案是使用Asynktask ..但它不與我工作,我不知道爲什麼..任何幫助,請您..ThankAsynctask與android中的webservices

public class CustomWebConnection extends AsyncTask<Void, Void, String> { 
private final String     uri = "http://localhost/mywebservices.php?op=GetAllRest"; 
private ArrayList<NameValuePair>  mParams; 
private OnPostExecuteListener  mPostExecuteListener = null; 

public static interface OnPostExecuteListener{ 
    void onPostExecute(String result); 
} 

CustomWebConnection(ArrayList<NameValuePair> nameValuePairs, 
    OnPostExecuteListener postExecuteListener) throws Exception { 

    mParams = nameValuePairs; 
    mPostExecuteListener = postExecuteListener; 
    if (mPostExecuteListener == null) 
     throw new Exception("Param cannot be null."); 
} 

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

     JSONObject param=null; 
     String result = null; 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpHost httpHost = new HttpHost("localhost",80); 
     HttpPost httpPost = new HttpPost(uri); 
     httpPost.addHeader("Content-Type", "application/json; charset=utf-8"); 


     try 
     { 
      //HttpEntity bodyEntity = new StringEntity(param.toString(), "utf8"); 
      //httpPost.setEntity(bodyEntity); 
      HttpResponse response = httpClient.execute(httpPost); 
      HttpEntity entity = response.getEntity(); 

      if (entity != null) { 
       InputStream instream = entity.getContent(); 
       BufferedReader reader = new BufferedReader(
        new InputStreamReader(instream)); 
       StringBuilder sb = new StringBuilder(); 


       String line = null; 
       while ((line = reader.readLine()) != null) 
        sb.append(line + "\n"); 


       result = sb.toString(); 
       instream.close();   
      } 
     } 

     catch(Exception e) 
     { 
      e.printStackTrace(); 
     } 


    httpPost.abort(); 
    return result ; 
} 

@Override 
protected void onPostExecute(String result) { 
    if (mPostExecuteListener != null){ 
     try { 
      JSONObject json = new JSONObject(result); 
      mPostExecuteListener.onPostExecute(result); 
     } catch (JSONException e){ 
      e.printStackTrace(); 
     } 
    } 
} 

,它是拋出此異常

 04-09 16:31:37.393: E/AndroidRuntime(2284): FATAL EXCEPTION: main 
    04-09 16:31:37.393: E/AndroidRuntime(2284): java.lang.NullPointerException 
     04-09 16:31:37.393: E/AndroidRuntime(2284): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116) 
    04-09 16:31:37.393: E/AndroidRuntime(2284): at org.json.JSONTokener.nextValue(JSONTokener.java:94) 
     04-09 16:31:37.393: E/AndroidRuntime(2284): at org.json.JSONObject.<init>(JSONObject.java:154) 
    04-09 16:31:37.393: E/AndroidRuntime(2284): at org.json.JSONObject.<init>(JSONObject.java:171) 
    04-09 16:31:37.393: E/AndroidRuntime(2284): at com.vtab.CustomWebConnection.onPostExecute(CustomWebConnection.java:92) 
04-09 16:31:37.393: E/AndroidRuntime(2284): at com.vtab.CustomWebConnection.onPostExecute(CustomWebConnection.java:1) 
04-09 16:31:37.393: E/AndroidRuntime(2284): at android.os.AsyncTask.finish(AsyncTask.java:602) 
04-09 16:31:37.393: E/AndroidRuntime(2284): at android.os.AsyncTask.access$600(AsyncTask.java:156) 
04-09 16:31:37.393: E/AndroidRuntime(2284): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615) 
04-09 16:31:37.393: E/AndroidRuntime(2284): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-09 16:31:37.393: E/AndroidRuntime(2284): at android.os.Looper.loop(Looper.java:137) 
    04-09 16:31:37.393: E/AndroidRuntime(2284): at android.app.ActivityThread.main(ActivityThread.java:4424) 
    04-09 16:31:37.393: E/AndroidRuntime(2284): at java.lang.reflect.Method.invokeNative(Native Method) 
    04-09 16:31:37.393: E/AndroidRuntime(2284): at java.lang.reflect.Method.invoke(Method.java:511) 
    04-09 16:31:37.393: E/AndroidRuntime(2284): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
    04-09 16:31:37.393: E/AndroidRuntime(2284): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
04-09 16:31:37.393: E/AndroidRuntime(2284): at dalvik.system.NativeStart.main(Native Method) 

HttpResponse response = httpClient.execute(httpPost);

錯誤

,這是從服務器

[{"rest_id":"1","rest_name":"Al Dabke","cuisine":"Lebanese","no_tables":"15","bg_img":"lebanon","rest_logo":"dabke"},{"rest_id":"4","rest_name":"MY KIND OF PLACE","cuisine":null,"no_tables":"10","bg_img":"kindofplacebg","rest_logo":"mykindofplacelogo"},{"rest_id":"5","rest_name":"LANTANIA","cuisine":"Thai","no_tables":"10","bg_img":"lantaniabg","rest_logo":"lantanialogo"},{"rest_id":"6","rest_name":"LEONARDO","cuisine":"italian","no_tables":"10","bg_img":"leonardobg","rest_logo":"leonardologo"},{"rest_id":"7","rest_name":"LE MARCHE ","cuisine":"French","no_tables":"10","bg_img":"lemarchebg","rest_logo":"lemarchelogo"},{"rest_id":"8","rest_name":"Marias","cuisine":"mexican","no_tables":"10","bg_img":"mariang","rest_logo":"marialogo"},{"rest_id":"9","rest_name":"NOBLE HOUSE","cuisine":"chinese","no_tables":"10","bg_img":"noblehousebg","rest_logo":"noblehouselogo"}] 

JSON響應任何幫助,請

回答

1

我最近也遇到過這個問題,我發現HttpClient是Android ICS中的問題,,,我使用的簡單黑客就是將min SDK版本從15改爲7,但它不是一個合適的解決方案。我遇到了博客的代碼

StrictMode.ThreadPolicy policy = new StrictMode. 
ThreadPolicy.Builder().permitAll().build(); 
StrictMode.setThreadPolicy(policy); 

把這個放在onCreate()方法已經解決了我的問題。可能是它可以幫助你。我仍然在尋找更多的選擇。

+0

我得到這個錯誤,我這就是爲什麼NullPointerException出現..我得到這個org.apache.http.conn.HttpHostConnectException:連接到http:// localhost拒絕..並且我不從我使用的模擬器運行一個設備..我該如何解決這個問題..謝謝 – Basant 2012-04-16 12:13:53

0

它看起來像在解析JSON的錯誤,雖然你沒有得到一個JSONException。

+0

那麼如何解決它或如何以正確的方式解析JSON – Basant 2012-04-09 14:57:14

+0

如果您發佈了JSON來驗證該怎麼辦?解決這個問題的正確方法是確保服務器是否發送有效的JSON,如果確實發生了這種情況。 – MikeHelland 2012-04-09 15:10:04

+0

我發佈的JSON,它是有效的..我不知道什麼是問題 – Basant 2012-04-09 20:57:51