0

我想以 JSON 的形式将数据发送到我的服务器。首先,它在不使用 AsyncTask 的情况下运行良好,但后来我遇到了一些错误,不得不更改我的类以使用 AsyncTask 来执行一些网络操作。但是,当我更改为使用 Asynctask 时,我得到了一个 HTTP 响应状态码 500。我没有更改类内部的任何代码行。我应该怎么办?

安卓代码

public class Register extends AsyncTask<String, Void, JSONObject> {
private static String registerURL = "";
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
private List<NameValuePair> params = null;
private AlertDialog.Builder ad;
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "Unknown";
private Context mContext;

public Register(Context context) {
    registerURL = "http://sit-edu4.sit.kmutt.ac.th/csc498/53270327/Boss/sftrip/index.php/register";
    params = new ArrayList<NameValuePair>();
    mContext = context;
    ad = new AlertDialog.Builder(mContext);
}

protected JSONObject doInBackground(String... strings) {
    params.add(new BasicNameValuePair("email", strings[0].toString()));
    params.add(new BasicNameValuePair("password", strings[1].toString()));
    params.add(new BasicNameValuePair("firstname", strings[2].toString()));
    params.add(new BasicNameValuePair("lastname", strings[3].toString()));
    params.add(new BasicNameValuePair("mobile", strings[4].toString()));
    params.add(new BasicNameValuePair("relativemobile1", strings[5].toString()));
    params.add(new BasicNameValuePair("relativemobile2", strings[6].toString()));
    params.add(new BasicNameValuePair("relativemobile3", strings[7].toString()));

    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(registerURL);
    // Making HTTP request
    try {
        // defaultHttpClient

        httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        StatusLine statusLine = httpResponse.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        } else {
            Log.e("Log", "Failed to download result.." + statusCode);
        }

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

    try {
        if (is != null) {
            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);
        } else {
            Log.e("Log", "Something wrong with IS");
        }
    } 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;
}

protected void onPostExecute(JSONObject json) {
    try {
        if (json.getString(KEY_SUCCESS) != null) {
            String res = json.getString(KEY_SUCCESS); 
            if(Integer.parseInt(res) == 1){
                // user successfully registred                      
                // Launch Dashboard Screen
                ad.setTitle("Congratulation!");
                ad.setMessage("You have registered successfully");
                ad.setPositiveButton("Login now!", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        Intent dashboard = new Intent(mContext, LoginActivity.class);
                        // Close all views before launching Dashboard
                        dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        mContext.startActivity(dashboard);
                        // Close Registration Screen
                        //finish();
                    }
                });
                ad.show();
            } else if (json.getString(KEY_ERROR).equals("1")){
                // Error in login
                String error_msg = json.getString(KEY_ERROR_MSG);
                ad.setTitle("Error! ");
                ad.setIcon(android.R.drawable.btn_star_big_on);
                ad.setPositiveButton("Close", null);
                ad.setMessage(error_msg);
                ad.show();
            } else {
                ad.setTitle("Error! ");
                ad.setIcon(android.R.drawable.btn_star_big_on);
                ad.setPositiveButton("Close", null);
                ad.setMessage("Something wrong");
                ad.show();
            }
        } else {
            ad.setTitle("Error! ");
            ad.setIcon(android.R.drawable.btn_star_big_on);
            ad.setPositiveButton("Close", null);
            ad.setMessage("Something wrong");
            ad.show();
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

}

服务器代码(使用 codeigniter)

class Register extends CI_Controller {
function __construct() {
    parent::__construct();
    /*
     load you helper library
     */
    /*
     load you model
     */
    $this -> load -> model('login_model');
}

function index() {
    $userEmail = $this->input->post("email");
    $userPass = $this->input->post("password");
    $userFirstName = $this->input->post("firstname");
    $userLastName = $this->input->post("lastname");
    $userMobile = $this->input->post("mobile");
    $userRM1 = $this->input->post("relativemobile1");
    $userRM2 = $this->input->post("relativemobile2");
    $userRM3 = $this->input->post("relativemobile3");

    $this->login_model->create_member($userEmail, $userPass, $userFirstName, $userLastName, $userMobile,
    $userRM1, $userRM2, $userRM3);

    $query = $this->login_model-> check_email($userEmail);
    if($query) {
        $arr['success'] = "1";
        $arr['error'] = "0";
        $arr['error_msg'] = "";
    } else {
        $arr['success'] = "0";
        $arr['error'] = "1";
        $arr['error_msg'] = "Something wrong with your registration";
    }
    echo json_encode($arr);
}

}

4

0 回答 0