1

在 Android 中,当我解析 json 数据时,我得到了对象,并且可以在 logcat 中看到。但是当我转换为 JSONArray,然后再转换为 getString() 时,它给出了 NULLPOINTEREXCEPTION。我想在我的edittext中显示这个json请帮助请任何人帮助我。

这是我的 json 结果表单 php

{"result":[{"uid":"1","name":"vipul","phone":"9772950005","sap":"500023675"}],"success":1}

注意:我得到 json 它只是如何获取该元素并显示它的问题。

这是我的 json,可以在 logcat 中看到

10-19 02:13:51.082: W/Trace(1267): Unexpected value from nativeGetEnabledTags: 0
10-19 02:13:51.152: D/This is json error(1267): {"success":1,"result":[{"uid":"1","sap":"500023675","phone":"9772950005","name":"vipul"}]}

我的 java 代码是 ::::::java 代码:::::

      public class search_view extends Activity{    
private ProgressDialog pDialog;
JSONObject result;
private static final String KEY_SUCCESS = "success";
private static final String KEY_NAME = "name";
private static final String KEY_PHONE = "phone";
private static final String TAG_RESULT = "result";
private static final String TAG_UID = "uid";
private static final String TAG_SAP = "sap";
public String sap="500023675";
EditText txtName,txtsap,txtPhone,txtuid;

      JSONParser jsonParser = new JSONParser();
      private static final String KEY_SEARCH_URL = "http://10.0.2.2:8080/android/search.php";

@Override
public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_item);
        txtsap = (EditText) findViewById(R.id.sap);
        txtsap.setText("This is onCreate");
        new searchall().execute();
}

class searchall extends AsyncTask<String, String, JSONObject>{
    JSONObject json;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(search_view.this);
        pDialog.setMessage("Searching... be patient..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();


    }


    @Override
    protected void onPostExecute(JSONObject result) {
        JSONArray resu;
        String id = "hello this is null";
        // TODO Auto-generated method stub
        //super.onPostExecute(result);
        try {
            resu=result.getJSONArray(TAG_RESULT);
        } catch (JSONException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            resu = null;
        }   

        for( int i = 0;i <= resu.length(); i++){try {
            id = resu.getString(i);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }}

                txtuid =(EditText)findViewById(R.id.uid);
                txtName = (EditText)findViewById(R.id.name);
                txtPhone = (EditText) findViewById(R.id.phone);
                txtsap = (EditText) findViewById(R.id.sap);
                txtuid.setText(id);
                txtName.setText(id);
                txtPhone.setText("phone");
                txtsap.setText("this is yes");





        pDialog.dismiss();
    }
    @Override
    protected JSONObject doInBackground(String... arg0) {
                // TODO Auto-generated method stub
                int success;                    
                    List<NameValuePair> lis = new ArrayList<NameValuePair>();
                    lis.add(new BasicNameValuePair("sap", sap));
                    JSONObject json = jsonParser.makeHttpRequest(KEY_SEARCH_URL, "GET", lis);
                    Log.d("This is json error", json.toString());
                    try {
                        success = json.getInt(KEY_SUCCESS);

                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    JSONObject res = null;
                    try {
                        res = json.getJSONObject(TAG_RESULT);
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    //obj = (JSONObject) res.get(j);
                return res;



}
}
}

我也在使用 AsyncTask 并在 onPostExecute() 上更改主 UI

问题是我无法显示它

这是我的::::::::::::::: LOG CAT:::::::::::;;

    10-19 02:13:54.232: E/AndroidRuntime(1267): FATAL EXCEPTION: main
10-19 02:13:54.232: E/AndroidRuntime(1267): java.lang.NullPointerException
10-19 02:13:54.232: E/AndroidRuntime(1267):     at com.god.upesgetall.search_view$searchall.onPostExecute(search_view.java:73)
10-19 02:13:54.232: E/AndroidRuntime(1267):     at com.god.upesgetall.search_view$searchall.onPostExecute(search_view.java:1)
10-19 02:13:54.232: E/AndroidRuntime(1267):     at android.os.AsyncTask.finish(AsyncTask.java:631)
10-19 02:13:54.232: E/AndroidRuntime(1267):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-19 02:13:54.232: E/AndroidRuntime(1267):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
10-19 02:13:54.232: E/AndroidRuntime(1267):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-19 02:13:54.232: E/AndroidRuntime(1267):     at android.os.Looper.loop(Looper.java:137)
10-19 02:13:54.232: E/AndroidRuntime(1267):     at android.app.ActivityThread.main(ActivityThread.java:5039)
10-19 02:13:54.232: E/AndroidRuntime(1267):     at java.lang.reflect.Method.invokeNative(Native Method)
10-19 02:13:54.232: E/AndroidRuntime(1267):     at java.lang.reflect.Method.invoke(Method.java:511)
10-19 02:13:54.232: E/AndroidRuntime(1267):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-19 02:13:54.232: E/AndroidRuntime(1267):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-19 02:13:54.232: E/AndroidRuntime(1267):     at dalvik.system.NativeStart.main(Native Method)

谢谢

请帮忙....

4

0 回答 0