我正在尝试在android中进行注册过程。但是,当我填写字段并单击我的注册按钮“发生不幸崩溃”时。在我发现的logcat中,来自JSONPerser.java的httpClient.execute(httpPost)无效。
任何人都可以帮助......
Register.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();
}
UserFunctions.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
public JSONObject getJSONFromUrl(String url,List params){
// 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;
}
答案 0 :(得分: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");
}