0

我正在尝试在android中进行注册过程。但是当我填写字段并单击我的注册按钮时,“不幸的崩溃”发生了。在 logcat 中,我发现 JSONPerser.java 中的 httpClient.execute(httpPost) 不起作用。

任何人都可以帮助....

注册.java

register.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String fNameVal = fname.getText().toString();
                String lNameVal = lname.getText().toString();
                String emailValue = email.getText().toString();
                String passwordValue = password.getText().toString();

                UserFunctions userFunction = new UserFunctions();
                JSONObject json = userFunction.registerUser(fNameVal, lNameVal, emailValue, passwordValue);

                try {



                    if (json.getString(KEY_SUCCESS) != null) {
                        /*registerErrorMsg.setText("");*/
                        String res = json.getString(KEY_SUCCESS); 
                        if(Integer.parseInt(res) == 1){
                            // user successfully registred
                            // Store user details in SQLite Database
                            DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                            JSONObject json_user = json.getJSONObject("user");

                            // Clear all previous data in database
                            userFunction.logoutUser(getApplicationContext());
                            db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_CREATED_AT));                        
                            // Launch Dashboard Screen
                            Toast.makeText(getApplicationContext(), "Registration Successful. Please Login.", Toast.LENGTH_LONG).show();
                            Intent dashboard = new Intent(getApplicationContext(), Login.class);
                            // Close all views before launching Dashboard
                            dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            startActivity(dashboard);
                            // Close Registration Screen
                            finish();
                        }else{
                            // Error in registration
                            /*registerErrorMsg.setText("Error occured in registration");*/
                            Toast.makeText(getApplicationContext(), "Error occured in registration", Toast.LENGTH_LONG).show();
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

用户函数.java

public JSONObject registerUser(String firstname, String lastname, String email, String password){
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", register_tag));
        params.add(new BasicNameValuePair("firstName", firstname));
        params.add(new BasicNameValuePair("lastName", lastname));
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("password", password));

        System.out.println(firstname + lastname + email + password + registerURL);

        // getting JSON Object
        JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
        // return json
        return json;
    }

JSONParser.java

公共 JSONObject getJSONFromUrl(字符串 url,列表参数){

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        System.out.println("HTTP POST: " + httpPost);
        httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        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;

}   
4

1 回答 1

2

将此行添加到getJSONFromUrl

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

并且在相同的功能中

while ((line = reader.readLine()) != null) {
    sb.append(line + "n");
}

将其更改为

while ((line = reader.readLine()) != null) {                
    sb.append(line + "\n");
}
于 2013-06-23T14:47:23.817 回答