我正在开发一个从PHP MysQl服务器收集某些信息的Android应用程序,但是我遇到了JSON Parser的问题,有时候它有效它不起作用,它给了我这些错误:
E/JSON Parser﹕ Error parsing data org.json.JSONException: Value Failed of type java.lang.String cannot be converted to JSONObject
Caused by: java.lang.NullPointerException
产生错误的代码:
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("fname", fname));
params.add(new BasicNameValuePair("lname", lname));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("city", city));
params.add(new BasicNameValuePair("nrc", nrc));
params.add(new BasicNameValuePair("dob", dob));
params.add(new BasicNameValuePair("cell", cell));
Log.d("username", username);
Log.d("password", password);
Log.d("fname", fname);
Log.d("lname",lname );
Log.d("email",email );
Log.d("city",city );
Log.d("nrc",nrc );
Log.d("dob",dob );
Log.d("cell",cell );
Log.d("request!", "starting");
//Posting user data to script
JSONObject json = jsonParser.makeHttpRequest(
REGISTER_URL, "POST", params);
// full json response
Log.d("Registering attempt", json.toString());
// json success element
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("User Profile Created!", json.toString());
NullException是由Line引起的:
Log.d("Registering attempt", json.toString());
答案 0 :(得分:0)
我认为这里的问题是你没有得到正确的JSON
字符串作为回复。确保行jsonParser.makeHttpRequest(REGISTER_URL, "POST", params);
返回正确的jSON字符串。在那里放置一个断点并尝试调试以检查您是否收到了正确的JSON
响应。希望它有所帮助。