I have made a signup form that sends the data taken from the signup function and post to the url(which is actually saved in another class) The problem is that the json object isnt being sent to the server. The reponse from the server states that " {"settings":{"success":"0","message":"Please enter a value for the first_name field.","fields":[]},"data":[]}"
Can someone please help me understand where have I gone wrong. Here are my code snippets:
public void signup() throws JSONException {
String firstname = edittext_fname.getText().toString();
String lastname = editext_lname.getText().toString();
String email = editext_email.getText().toString();
String mobile = editext_mobile.getText().toString();
String pass = editext_pass.getText().toString();
String username = editext_user.getText().toString();
String address = editext_add.getText().toString();
String cityname = editText_city.getText().toString();
String zipcode = editText_Zip.getText().toString();
String city_id = editText_cityid.getText().toString();
String birthdate = textView_birth.getText().toString();
String statename = textView_state.getText().toString();
String stateid = state_id;
JSONObject jsonObject = new JSONObject();
jsonObject.put("first_name", firstname);
jsonObject.put("last_name", lastname);
jsonObject.put("birth_date", birthdate);
jsonObject.put("email", email);
jsonObject.put("user_name", username);
jsonObject.put("password", pass);
jsonObject.put("mobile_no", mobile);
jsonObject.put("address", address);
jsonObject.put("zip_code", zipcode);
jsonObject.put("city_id", city_id);
jsonObject.put("city", cityname);
jsonObject.put("state_id", stateid);
jsonObject.put("reference_name", "xxx");
jsonObject.put("country_id", "223");
jsonObject.put("refer_by", "others");
jsonObject.put("user_role_type", "3");
if (jsonObject.length() > 0) {
new sendJsonData().execute(String.valueOf(jsonObject));
}
}
Async class that sets up HttpUrlConnection and appends json object
private class sendJsonData extends AsyncTask<String, Void, String> {
ProgressDialog progressDialog;
String Jsonresponse = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(FirstScreenActivity.this);
progressDialog.setMessage("please wait");
progressDialog.setCancelable(false);
progressDialog.show();
}
@Override
protected String doInBackground(String... params) {
String Jsondata = params[0];
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
try {
URL url = new URL(WsUtils.BASE_URL+WsUtils.SIGNUP);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setConnectTimeout(10000);
urlConnection.setRequestProperty("Accept","application/json");
urlConnection.setRequestProperty("Content-Type","application/json");
//Writer writer = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8"));
//writer.write(Jsondata);
// writer.close();
DataOutputStream printout = new DataOutputStream(urlConnection.getOutputStream ());
printout.writeBytes("PostData=" + Jsondata);
printout.writeBytes(Jsondata);
Log.e("json", Jsondata);
// printout.flush ();
// printout.close ();
DataInputStream in = new DataInputStream(urlConnection.getInputStream());
Jsonresponse = convertStreamToString(in);
return Jsonresponse;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
progressDialog.dismiss();
Log.e("response", Jsonresponse);
}
}
This is just to get the input (response) to string and get display!
public String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}