【发布时间】:2017-03-02 09:55:38
【问题描述】:
我制作了一个注册表单,发送从注册函数获取的数据并发布到 url(实际上保存在另一个类中) 问题是 json 对象没有被发送到服务器。来自服务器的响应指出“ {"settings":{"success":"0","message":"请输入 first_name 字段的值。","fields":[]},"data": []}"
有人可以帮我理解我哪里出错了。 这是我的代码 sn-ps:
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));
}
}
设置 HttpUrlConnection 并附加 json 对象的异步类
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);
}
}
这只是为了获取输入(响应)到字符串并得到显示!
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();
}
【问题讨论】:
-
你能告诉我们你得到的回应吗
-
响应:{"settings":{"success":"0","message":"请输入 first_name 字段的值。","fields":[]},"data ":[]}
-
我认为名字为空。发送前是否检查了名字是否为空?
-
查看我的 json 对象,在进入 doinbackground 后在日志中收到:::::::::::::::::::>>>>E/json: {"first_name":"sana","last_name":"Baid","birth_date":"7-3-2017","email":"s@h.com","user_name":"sbaid","密码":"Da1=","mobile_no":"9898813889","address":"fdfd","zip_code"
标签: android json web-services