JSON网址不起作用

时间:2011-05-02 18:06:32

标签: java php android

我想从服务器调用json数组

此行适用于我

JSONObject json = JSONfunctions.getJSONfromURL("http://midsweden.gofreeserve.com/fetch.php");

但是当我尝试使用我的sharedprefences类获取时它不起作用并且强制要停止,这里是代码

 JSONObject json = JSONfunctions.getJSONfromURL("http://midsweden.gofreeserve.com/fetch.php?identifier="
                + Test.this.myprefs.getEmail());

在eclipse中,它没有指出任何错误,我的意思是缺少var减速但它不起作用。任何建议???

以下是我在logcat中看到的内容

05-02 19:49:53.569: ERROR/AndroidRuntime(20721): Uncaught handler: thread main exiting due to uncaught exception
05-02 19:49:53.688: ERROR/AndroidRuntime(20721): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.droidnova.android.howto.optionmenu/com.droidnova.android.howto.optionmenu.Test}: java.lang.NullPointerException
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at android.app.ActivityThread.access$2200(ActivityThread.java:126)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at android.os.Looper.loop(Looper.java:123)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at android.app.ActivityThread.main(ActivityThread.java:4595)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at java.lang.reflect.Method.invokeNative(Native Method)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at java.lang.reflect.Method.invoke(Method.java:521)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at dalvik.system.NativeStart.main(Native Method)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721): Caused by: java.lang.NullPointerException
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at com.droidnova.android.howto.optionmenu.Test.onCreate(Test.java:37)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     ... 11 more
05-02 19:49:53.778: ERROR/ActivityManager(97): fail to set top app changed!
05-02 19:49:58.048: ERROR/PackageInstallationReceiver(248): Remove /data/local/tmp/com.droidnova.android.howto.optionmenu.apk Fail!
05-02 19:51:41.418: ERROR/AndroidRuntime(20746): ERROR: thread attach failed
05-02 19:51:42.818: ERROR/AndroidRuntime(20754): ERROR: thread attach failed
05-02 19:51:43.438: ERROR/AndroidRuntime(20762): Uncaught handler: thread main exiting due to uncaught exception
05-02 19:51:43.448: ERROR/AndroidRuntime(20762): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.droidnova.android.howto.optionmenu/com.droidnova.android.howto.optionmenu.Test}: java.lang.NullPointerException
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at android.app.ActivityThread.access$2200(ActivityThread.java:126)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at android.os.Looper.loop(Looper.java:123)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at android.app.ActivityThread.main(ActivityThread.java:4595)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at java.lang.reflect.Method.invokeNative(Native Method)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at java.lang.reflect.Method.invoke(Method.java:521)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at dalvik.system.NativeStart.main(Native Method)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762): Caused by: java.lang.NullPointerException
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at com.droidnova.android.howto.optionmenu.Test.onCreate(Test.java:37)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
05-02 19:51:43.448: ERROR/AndroidRuntime(20762):     ... 11 more
05-02 19:51:45.178: ERROR/ActivityManager(97): fail to set top app changed!
05-02 19:51:54.428: ERROR/AndroidRuntime(20772): ERROR: thread attach failed
05-02 19:51:55.818: ERROR/AndroidRuntime(20780): ERROR: thread attach failed
05-02 19:51:56.528: ERROR/AndroidRuntime(20788): Uncaught handler: thread main exiting due to uncaught exception
05-02 19:51:56.538: ERROR/AndroidRuntime(20788): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.droidnova.android.howto.optionmenu/com.droidnova.android.howto.optionmenu.Test}: java.lang.NullPointerException
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at android.app.ActivityThread.access$2200(ActivityThread.java:126)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at android.os.Looper.loop(Looper.java:123)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at android.app.ActivityThread.main(ActivityThread.java:4595)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at java.lang.reflect.Method.invokeNative(Native Method)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at java.lang.reflect.Method.invoke(Method.java:521)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at dalvik.system.NativeStart.main(Native Method)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788): Caused by: java.lang.NullPointerException
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at com.droidnova.android.howto.optionmenu.Test.onCreate(Test.java:37)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2544)
05-02 19:51:56.538: ERROR/AndroidRuntime(20788):     ... 11 more
05-02 19:51:58.658: ERROR/ActivityManager(97): fail to set top app changed!
05-02 19:55:31.438: ERROR/AndroidRuntime(20809): ERROR: thread attach failed
05-02 19:55:38.148: ERROR/AndroidRuntime(20820): ERROR: thread attach failed
05-02 19:55:41.638: ERROR/PackageInstallationReceiver(248): Remove /data/local/tmp/com.droidnova.android.howto.optionmenu.apk Fail!
05-02 20:15:28.608: ERROR/wpa_supplicant(139): Set_key: Wrong Key
05-02 20:15:28.608: ERROR/wpa_supplicant(139): Set_key: Wrong Key
05-02 20:15:28.608: ERROR/wpa_supplicant(139): Set_key: Wrong Key
05-02 20:15:28.608: ERROR/wpa_supplicant(139): Set_key: Wrong Key
05-02 20:15:28.608: ERROR/wpa_supplicant(139): Set_key: Wrong Key
05-02 20:15:29.648: ERROR/wpa_supplicant(139): prepare_filter_struct: type=5
05-02 20:15:32.728: ERROR/wpa_supplicant(139): prepare_filter_struct: type=5

此处更详细的是测试类

public class Test extends ListActivity  {
     Prefs myprefs = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listplaceholder);
        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

        JSONObject json = JSONfunctions.getJSONfromURL("http://midsweden.gofreeserve.com/fetch.php?identifier="
            + Test.this.myprefs.getEmail());

        try{

            JSONArray  earthquakes = json.getJSONArray("earthquakes");

            for(int i=0;i<earthquakes.length();i++){                        
                HashMap<String, String> map = new HashMap<String, String>();    
                JSONObject e = earthquakes.getJSONObject(i);

                map.put("id",  String.valueOf(i));
                map.put("name", "Earthquake name:" + e.getString("name"));
                map.put("password", "Magnitude: " +  e.getString("password"));
                mylist.add(map);            
            }       
        }catch(JSONException e)        {
             Log.e("log_tag", "Error parsing data "+e.toString());
        }

        ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.test, 
                        new String[] { "name", "magnitude" }, 
                        new int[] { R.id.item_title, R.id.item_subtitle });

        setListAdapter(adapter);

        final ListView lv = getListView();
        lv.setTextFilterEnabled(true);  
        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
                @SuppressWarnings("unchecked")
                HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   
                Toast.makeText(Test.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show(); 

            }
        });
    }

这是分享偏好类

package com.droidnova.android.howto.optionmenu;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class Prefs {

    private SharedPreferences _prefs = null;
    private Editor _editor = null;
    private String _useremailaddress = "Unknown";
    private String _serverurl = "http://chinar.gofreeserve.com/db.php";

    public Prefs(Context context) {
        this._prefs = context.getSharedPreferences("PREFS_PRIVATE", Context.MODE_PRIVATE);
        this._editor = this._prefs.edit();
    }

    public String getValue(String key, String defaultvalue) {
        if (this._prefs == null) {
            return "Unknown";
        }

        return this._prefs.getString(key, defaultvalue);
    }

    public void setValue(String key, String value) {
        if (this._editor == null) {
            return;
        }

        this._editor.putString(key, value);

    }

    public String getEmail() {
        if (this._prefs == null) {
            return "Unknown";
        }

        this._useremailaddress = this._prefs.getString("emailaddress", "noidea");
        return this._useremailaddress;
    }

    public String getServer() {
        if (this._prefs == null) {
            return "http://chinar.gofreeserve.com";
        }

        this._serverurl = this._prefs.getString("serverurl", "http://chinar.gofreeserve.com/");
        return this._serverurl;
    }

    public void setEmail(String newemail) {
        if (this._editor == null) {
            return;
        }

        this._editor.putString("emailaddress", newemail);
    }

    public void setServer(String serverurl) {
        if (this._editor == null) {
            return;
        }
        this._editor.putString("serverurl", serverurl);
    }

    public void save() {
        if (this._editor == null) {
            return;
        }
        this._editor.commit();
    }
}

3 个答案:

答案 0 :(得分:2)

您收到NullPointerException,因为(很可能)您的myprefs为空。

您在哪里初始化此字段?

无论如何,为了保存引用,您应该查看Shared Preferences而不是创建自己的解决方案。

答案 1 :(得分:1)

myPrefs var为null

 public class Test extends ListActivity  {
 Prefs myprefs = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.listplaceholder);
    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

 //myprefs can only be null at this point, you need this line or an equivalent for your Prefs class:
 myprefs = PreferenceManager.getDefaultSharedPreferences(this); 

    JSONObject json = JSONfunctions.getJSONfromURL("http://midsweden.gofreeserve.com/fetch.php?identifier="
        + Test.this.myprefs.getEmail());

API:

getDEfaultSharedPrefs()

getSharedPrefs()

修改

啊,是的,你使用了一些自定义首选项类,所以你必须实例化这个类。

  // Your choice of
  Prefs myprefs = new Prefs();
  Prefs myprefs = new Prefs(this);
  Prefs myprefs = new Prefs(getApplicationContext());

getApplicationContext()

或者如果他想要使用的方法是静态的:

  Prefs.getEmail();     

请仔细阅读:Top Ten Java Errors 10号和1号!)

答案 2 :(得分:0)

您的代码中有一个空指针异常:

Caused by: java.lang.NullPointerException
05-02 19:49:53.688: ERROR/AndroidRuntime(20721):     at com.droidnova.android.howto.optionmenu.Test.onCreate(Test.java:37)

修复此问题,至少应用程序将运行。