JSON android problem !
This program wants to connect to web server in this case i use WAMP then i create file food.php in www, and i connect my request from activity to get data from database (WAMP).
When i run it force to close ! WHY ? Here the code !!
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.a开发者_如何学JAVApache.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();
}
}
}
HERE my 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>
Here is log cat :
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
You should not create json array from result but create json object from result and then take array from it. Here you can find more info: http://www.androidcompetencycenter.com/2009/10/json-parsing-in-android/
精彩评论