2011-07-13 18 views
0

這個程序想要連接到web服務器,在這種情況下,我使用WAMP,然後我在www中創建文件food.php,並將活動的請求連接到從數據庫(WAMP)獲取數據。JSON android問題!

當我運行它迫使關閉!爲什麼?這裏的代碼!

JSON.java

package org.example; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.app.ListActivity; 
import android.net.ParseException; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.Toast; 

public class JSON extends ListActivity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    String result = null; 
    InputStream is = null; 
    StringBuilder sb=null; 

    //http post 
    try{ 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http://127.0.0.1/food.php"); 

     List<NameValuePair> nvp = new ArrayList<NameValuePair>(); 

     httppost.setEntity(new UrlEncodedFormEntity(nvp)); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 
    }catch(Exception e){ 
     Log.e("log_tag", "Error in http connection"+e.toString()); 
    } 

    //convert response to string 
    try{ 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
     sb = new StringBuilder(); 
     sb.append(reader.readLine() + "\n"); 
     String line="0"; 

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

     is.close(); 
     result=sb.toString(); 

    }catch(Exception e){ 
     Log.e("log_tag", "Error converting result "+e.toString()); 
    } 

    //paring data 
    int fd_id; 
    String fd_name; 
    try{ 
     JSONArray jArray = new JSONArray(result); 
     JSONObject json_data=null; 

     for(int i=0;i<jArray.length();i++){ 
      json_data = jArray.getJSONObject(i); 
      fd_id=json_data.getInt("FOOD_ID"); 
      fd_name=json_data.getString("FOOD_NAME"); 
     } 

    }catch(JSONException e1){ 
     Toast.makeText(getBaseContext(), "No Food Found", Toast.LENGTH_LONG).show(); 
    }catch (ParseException e1){ 
     e1.printStackTrace(); 
    } 
} 

}

在這裏,我的main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<ListView 
android:id="@android:id/list" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"/> 

<TextView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello" 
/> 
</LinearLayout> 

這裏是日誌的貓:

07-13 10:21:41.324: ERROR/AndroidRuntime(280): FATAL EXCEPTION: main 
07-13 10:21:41.324: ERROR/AndroidRuntime(280): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example/org.example.JSON}: java.lang.NullPointerException 
07-13 10:21:41.324: ERROR/AndroidRuntime(280):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
07-13 10:21:41.324: ERROR/AndroidRuntime(280):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
07-13 10:21:41.324: ERROR/AndroidRuntime(280):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
07-13 10:21:41.324: ERROR/AndroidRuntime(280):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
07-13 10:21:41.324: ERROR/AndroidRuntime(280):  at android.os.Handler.dispatchMessage(Handler.java:99) 
07-13 10:21:41.324: ERROR/AndroidRuntime(280):  at android.os.Looper.loop(Looper.java:123) 
07-13 10:21:41.324: ERROR/AndroidRuntime(280):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
07-13 10:21:41.324: ERROR/AndroidRuntime(280):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-13 10:21:41.324: ERROR/AndroidRuntime(280):  at java.lang.reflect.Method.invoke(Method.java:521) 
07-13 10:21:41.324: ERROR/AndroidRuntime(280):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
07-13 10:21:41.324: ERROR/AndroidRuntime(280):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
07-13 10:21:41.324: ERROR/AndroidRuntime(280):  at dalvik.system.NativeStart.main(Native Method) 
07-13 10:21:41.324: ERROR/AndroidRuntime(280): Caused by: java.lang.NullPointerException 
07-13 10:21:41.324: ERROR/AndroidRuntime(280):  at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112) 
07-13 10:21:41.324: ERROR/AndroidRuntime(280):  at org.json.JSONTokener.nextValue(JSONTokener.java:90) 
07-13 10:21:41.324: ERROR/AndroidRuntime(280):  at org.json.JSONArray.<init>(JSONArray.java:87) 
07-13 10:21:41.324: ERROR/AndroidRuntime(280):  at org.json.JSONArray.<init>(JSONArray.java:103) 
07-13 10:21:41.324: ERROR/AndroidRuntime(280):  at org.example.JSON.onCreate(JSON.java:74) 
07-13 10:21:41.324: ERROR/AndroidRuntime(280):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
07-13 10:21:41.324: ERROR/AndroidRuntime(280):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
07-13 10:21:41.324: ERROR/AndroidRuntime(280):  ... 11 more 
+0

粘貼logcat跟蹤夥計... – Cristian

+0

感嘆號讓我覺得我迫切需要點擊這個問題;但唉,沒有堆棧跟蹤。請給我們一些東西來處理,告訴我們什麼是例外,以及它發生的是什麼。另外,如果它實際上是'JSONException'(這不僅僅是對'Activity'的引用),它將有助於添加您試圖解析的JSON。 –

+0

我已經爲你寫了日誌貓thx :) – sayvortana

回答