1

有一个JSON输出:

{"status":"error","data":null,"message":"required parameters not available."}

要访问它,有一个JSON Parsing java 类和一个User Function java 类。

我在JSONAsyncTask 中的 LoginActivity 上访问此文件时感到困惑。该任务引发空指针异常。我真的认为不提及“状态”是造成这种情况的原因……但这就是真正的原因吗?我是android新手,所以我没有好主意。请帮忙。代码如下:

JSONParser.java

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
    static int GET = '1';
    static int POST ='2';

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url, List<NameValuePair> params, int method) throws URISyntaxException {

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse httpResponse = null;
        HttpEntity httpentity = null;

        try {
            switch (method) {
            case 1: 
                if(params!=null){
                    String paramString = URLEncodedUtils.format(params, "utf-8");
                    url += "?" + paramString;   
                }

                Log.e("URL", url);
                HttpGet httpGet = new HttpGet(url);
                httpResponse = httpClient.execute(httpGet);
                httpentity = httpResponse.getEntity();          

                break;
            case 2: 
                HttpPost postrequest = new HttpPost(url);
                postrequest.setEntity(new UrlEncodedFormEntity(params));
                httpResponse = httpClient.execute(postrequest);
                httpentity = httpResponse.getEntity();

                break;
            }

            is = httpentity.getContent();


        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
            Log.e("JSON", json);
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);            
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }

}   

用户函数.java

public class UserFunctions {

    private JSONParser jsonParser;
    private static String url_login = "http://10.0.0.2/blamethestars2/public/api/user/login";

    static int GET = '1';
    static int POST ='2';

    // constructor
    public UserFunctions(){
        jsonParser = new JSONParser();
    }

    public JSONObject login(String username, String password){
        JSONObject json = null;

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("email", username));
        params.add(new BasicNameValuePair("password", password));
        try {
            json = jsonParser.getJSONFromUrl(url_login, params, POST);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return json;

    }
}

登录活动.java

public class LoginActivity extends Activity{

    EditText username, password;
    String user, pass;
    Button submit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sign_in);
        username = (EditText)findViewById(R.id.user_email);
        password = (EditText)findViewById(R.id.user_password);


        submit = (Button)findViewById(R.id.user_submit);


        submit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                user = username.getText().toString();
                pass = password.getText().toString();
                if(user.length()<1 || pass.length()<1){
                    Toast.makeText(getApplicationContext(), "fill user name and pass", Toast.LENGTH_SHORT).show();
                }else{
                    new Asynconnection().execute();
               }
            }
        });
    }   

    public class Asynconnection extends AsyncTask<Void, Void, Void>{
        JSONObject jsarray;
        @Override
        protected Void doInBackground(Void... params) {
            UserFunctions users = new UserFunctions();
            jsarray = users.login(user, pass);      

            return null;
        }
    }

}   

现在我只是想检查 api 响应是否出现在 logcat 上

日志错误:

04-18 16:07:01.190: W/System.err(874): java.lang.NullPointerException
04-18 16:07:01.260: W/System.err(874):  at com.example.btshome.library.JSONParser.getJSONFromUrl(JSONParser.java:66)
04-18 16:07:01.260: W/System.err(874):  at com.example.btshome.library.UserFunctions.login(UserFunctions.java:38)
04-18 16:07:01.270: W/System.err(874):  at com.example.btshome.LoginActivity$Asynconnection.doInBackground(LoginActivity.java:54)
04-18 16:07:01.270: W/System.err(874):  at com.example.btshome.LoginActivity$Asynconnection.doInBackground(LoginActivity.java:1)
04-18 16:07:01.280: W/System.err(874):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-18 16:07:01.280: W/System.err(874):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-18 16:07:01.280: W/System.err(874):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-18 16:07:01.293: W/System.err(874):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-18 16:07:01.293: W/System.err(874):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-18 16:07:01.300: W/System.err(874):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-18 16:07:01.320: W/System.err(874):  at java.lang.Thread.run(Thread.java:856)
4

0 回答 0