2012-04-09 10 views
0

Ich brauche Hilfe .. Ich versuche, PHP Webservices von meinem Android-Client-Anwendung zu nennen, und ich konnte es nicht vom Haupt-Thread nennen coz das SDK ist 15, und ich suche im Internet und ich fand die Lösung für dieses Problem, die Asynktask zu verwenden ist .. aber es nicht mit mir arbeiten, ich weiß nicht, warum .. jede Hilfe Sie bitte .THANKAsynctask mit Webservices in Android

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

und es ist diese Ausnahme werfen

 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) 

der Fehler bei HttpResponse response = httpClient.execute(httpPost);

und dies ist die JSON Antwort vom Server

[{"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"}] 

jede mögliche Hilfe bitte

Antwort

1

Ich habe auch dieses Problem vor kurzem und ich fand, dass HttpClient Problem in Android ICS ist ,,, Der einfache Hack, den ich verwendete, ist min SDK-Version zu 7 von 15 zu ändern. Aber es ist nicht eine richtige Lösung. ich komme über den Code von einem Blog

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

Putting dies in onCreate() -Methode mein Problem gelöst hat. Vielleicht kann es dir helfen. Ich suche noch nach weiteren Optionen.

+0

Ich habe diesen Fehler und ich dies ist der Grund, warum die NullPointerException erscheint .. Ich habe diese org.apache.http.conn.HttpHostConnectException: Verbindung zu http: // localhost abgelehnt .. und ich laufe nicht von Emulator ich verwendet ein gerät .. wie kann ich dieses problem lösen..danke ihnen – Basant

0

Es sieht aus wie der Fehler beim Parsen der JSON war, obwohl Sie keinen JSONException bekommen.

+0

also, wie man es repariert oder wie man den JSON richtig analysiert – Basant

+0

Was, wenn Sie den JSON bekanntgaben, um zu überprüfen? Der richtige Weg, um es zu beheben, wäre, den Server beim Senden von gültigem JSON zu überprüfen, falls das tatsächlich der Fall ist. – MikeHelland

+0

Ich poste das JSON und es ist gültig .. Ich weiß nicht, was das Problem ist – Basant