nullpointer异常来自onpostexcute

时间:2015-08-11 09:34:13

标签: java android android-asynctask nullpointerexception

Plz帮助我 当我们从服务器获取数据并保存在字符串变量中时,它存储在变量中但不能反转。

public class Profile extends Activity {
    ListView list;
    Activity act;
    String[] username = { "Pankaj", "Aaa" };
    TextView name;
    JSONObject object;
    String url = "http://thinksl.com/taughtable/profile.php?user_email=pank@gmail.com";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile);
        list = (ListView) findViewById(R.id.list);
        name = (TextView) findViewById(R.id.mailid);
        Custom_Profile pro = new Custom_Profile(Profile.this, username);
        list.setAdapter(pro);
        new Userdata().execute(url);
    }

    public class Userdata extends AsyncTask<String, Void, String> {
        public static final int connection_type = 1500;
        Profile p = new Profile();
        ProgressDialog dialog;
        String resul;
        HashMap<String, String> user;

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            dialog = new ProgressDialog(Profile.this);
            dialog.setTitle("Processing");
            dialog.setMessage("Loading Data.Please wait....");
            dialog.setCancelable(false);
            dialog.show();

        }

        @Override
        protected String doInBackground(String... url) {
            // TODO Auto-generated method stub
            Log.e("result", "Do in background");
            HttpParams params = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(params, connection_type);
            HttpConnectionParams.setSoTimeout(params, connection_type);
            HttpClient client = new DefaultHttpClient(params);

            ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
            param.add(new BasicNameValuePair("user_email", "pank@gmail.com"));

            HttpGet get = new HttpGet(url[0]);
            try {
                Log.e("result", "Try");
                HttpResponse response = client.execute(get);
                Log.e("result", "Response" + response);
                HttpEntity entity = response.getEntity();
                String result = EntityUtils.toString(entity);
                Log.e("result", "result" + result);
                JSONObject jobject = new JSONObject(result);
                object = jobject.getJSONObject("userdetail");
                Log.e("result", "result" + object);

                String name = object.getString("user_login");
                int post = object.getInt("month");
                Log.e("name", "name" + name);
                Log.e("name", "post" + post);
                Log.e("result", "result" + resul);
                user = new HashMap<String, String>();
                user.put("name", name);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            dialog.dismiss();
            String s = user.get("name").toString();
            p.name.setText(s);

        }
    }
}

2 个答案:

答案 0 :(得分:0)

使用此代码

JSONObject res = new JSONObject(result);
JSONObject response = res.getJSONObject("userdetail");
String niceName = response.getString("user_nicename");
String displayName = response.getString("display_name");

答案 1 :(得分:0)

问题是你总是返回null,在捕获之后你只有返回。

查看doinbackground方法的最后一行,&#34;返回null&#34;。

所以改为:

        HttpGet get = new HttpGet(url[0]);
            HttpResponse response = client.execute(get);
            Log.e("result", "Response" + response);
            HttpEntity entity = response.getEntity();
            String result = EntityUtils.toString(entity);
            return result;

在postexecute方法中执行:

       @Override
        protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
       super.onPostExecute(result);
        try {
            JSONObject jobject = new JSONObject(result);
            object = jobject.getJSONObject("userdetail");
            Log.e("result", "result" + object);

            String name = object.getString("user_login");
            int post = object.getInt("month");
            Log.e("name", "name" + name);
            Log.e("name", "post" + post);
            Log.e("result", "result" + resul);
            user = new HashMap<String, String>();
            user.put("name", name);
            dialog.dismiss();
            String s = user.get("name").toString();
            p.name.setText(s);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }          
    }