我想通过网络服务访问Android应用。在Web服务中,执行新注册。 在Android应用程序中,进行新注册的xml文件。数据在SQL Server数据库中成功保存,并由Web服务正确保存,返回数据以jason字符串形式获取。但是当字符串转换为JSONObject时,它会给出如下错误:
Error parsing data org.json.JSONException: Value [{"userid":105,"created_at":"03-Oct-2013","success":1,"email":"rty@gmail.com","password":"rty12345","name":"rtyu"}] of type org.json.JSONArray cannot be converted to JSONObject
我已将注册活动注册为RegisterActivity.java
else
{
erName.setText("");
erPass.setText("");
erEmail.setText("");
erCopass.setText("");
UserFunction userFunction = new UserFunction();
JSONObject json = userFunction.registerUser(name, email, password);
// check for login response
try {
if (json.getString(KEY_SUCCESS) != null) {
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_EMAIL),
json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent login = new Intent(getApplicationContext(), LoginActivity.class);
// Close all views before launching Dashboard
login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(login);
// Close Registration Screen
Toast.makeText(RegisterActivity.this,"You are Registered successfully",Toast.LENGTH_SHORT).show();
finish();
}else{
// Error in registration
Toast.makeText(RegisterActivity.this,"User Allready Registered!!!",Toast.LENGTH_LONG).show();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
因为错误发生在以下行:
if (json.getString(KEY_SUCCESS) != null)
在JSONParser类中,jObj获取null值。问题在于:jObj = new JSONObject(json);
JSONParser类的代码:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
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;
}
}
服务调用的另一类UserFunction: 公共类UserFunction {
private JSONParser jsonParser;
// Testing in localhost using wamp or xampp
// use http://10.0.2.2/ to connect to your localhost ie http://localhost/
private static String loginURL = "http://192.168.1.120/rvAndroidServices.ashx";
private static String registerURL = "http://192.168.1.120/rvAndroidServices.ashx";
private static String name1 = "http://192.168.1.120/rvAndroidServices.ashx";
private static String login_tag = "login";
private static String register_tag = "register";
private static String name_tag = "name";
// constructor
public UserFunction(){
jsonParser = new JSONParser();
}
/**
* function make Login Request
* @param email
* @param password
* */
public JSONObject loginUser(String email, String password){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", login_tag));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
jsonParser= new JSONParser();
JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
// return json
// Log.e("JSON", json.toString());
return json;
}
/**
* function make Login Request
* @param name
* @param email
* @param password
* */
public JSONObject registerUser(String name, String email, String password){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", register_tag));
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
jsonParser = new JSONParser();
// getting JSON Object
JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
// return json
return json;
}
/**
* Function get Login status
* */
public boolean isUserLoggedIn(Context context){
Databasehandler db = new Databasehandler(context);
int count = db.getRowCount();
if(count > 0){
// user logged in
return true;
}
return false;
}
public String getAppCategorydetail(Context context){
Databasehandler db = new Databasehandler(context);
String count = db.getAppCategorydetail();
return count;
}
/**
* Function to logout user
* Reset Database
* */
public boolean logoutUser(Context context){
Databasehandler db = new Databasehandler(context);
db.resetTables();
return true;
}
public JSONObject chname(String name)
{
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", name_tag));
params.add(new BasicNameValuePair("name", name));
JSONObject json = jsonParser.getJSONFromUrl(name1, params);
return json;
}
}
答案 0 :(得分:3)
在响应字符串中,你得到jsonArray而不是JsonObject,所以当你说
时try {
jObj = new JSONObject(json);
}
这里json需要在JsonArray中转换而不是JSONObject。之后从json数组中获取第一个对象。
类似的东西:
try {
JSONArray jArr = new JSONArray(json);
JSONObject jObj = jArr.getJSONObject(0);
}
希望这有帮助!
答案 1 :(得分:2)
Error parsing data org.json.JSONException: Value [{"userid":105,"created_at":"03-Oct-2013","success":1,"email":"rty@gmail.com","password":"rty12345","name":"rtyu"}] of type org.json.JSONArray cannot be converted to JSONObject
您的例外解释一切
您的字符串为JSONArray not JSONObject
,您需要从JSONObject
获取JSONArray
。
因此,使用JSONArray
获取JSONOBbject
更改为:
jObj = new JSONArray(json).getJSONObject(0);
答案 2 :(得分:0)
试试这个......
JSONArray data = jsonObj.getJSONArray("data");
答案 3 :(得分:0)
您的网络服务(apis)可能无法将数据返回为&#34; json&#34; 如果apis是用php编写的 - 尝试在下面添加一行
标题(&#39;内容类型:application / json&#39;);
它应该有用。