JSON解析返回空值 [英] Json parsing return null value
本文介绍了JSON解析返回空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的Android应用程序中,我使用json解析进行登录.Json解析的URL包含如下浏览器中的json数据
In my android application i am using login with json parsing.Json parsing url contains json data on browser like below
{
posts: [{
status: 1,
user_id: 10,
access_token: "BRBL4JJXB9",
image: "images/users/.PNG",
msg: "Login Successful."
}]
}
但是当调用相同的URL进行JSON解析时,它返回null.我的解析代码如下
but while call same URL for JSON parsing it returns null.My parsing code below
@Override
protected Void doInBackground(Void... params) {
String url = "";
url = URLS.BASEURL + "mobile_api.php?action=userLogin&user_name=" +plusname+"&image="+plusimage+"&email="+URLEncoder.encode(gmail) +"&gcm_id="+deviceid;
JSONReader reader = new JSONReader();
json = getJsonGET(url);
if (json != null) {
try {
JSONObject response = new JSONObject(json);
JSONArray arr = response.getJSONArray("posts");
for (int index = 0; index < arr.length(); index++) {
JSONObject jobj = arr.getJSONObject(index);
status = jobj.getString("status");
if (status.equalsIgnoreCase("1")) {
userid = jobj.getString("user_id");
accesstoken = jobj.getString("access_token");
SharedPreferences.Editor edit = m_pref.edit();
edit.putString("userid", userid);
edit.putString("accesstoken", accesstoken);
edit.commit();
} else {
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
private String getJsonGET(String url) {
String contentAsString;
StringBuilder total = new StringBuilder();
try {
InputStream is = null;
try {
URL url2 = new URL(url);
HttpURLConnection conn = (HttpURLConnection) url2.openConnection();
conn.setReadTimeout(100000 /* milliseconds */);
conn.setConnectTimeout(150000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
int response = conn.getResponseCode();
Log.d("", "The response is: " + response);
is = conn.getInputStream();
// Convert the InputStream into a string
Reader reader = null;
reader = new InputStreamReader(is, "UTF-8");
BufferedReader r = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
char[] buffer = new char[total.length()];
reader.read(buffer);
contentAsString = new String(buffer);
} finally {
if (is != null) {
is.close();
}
}
} catch (Exception e) {
return null;
}
return total.toString();
}
变量"json"返回null.请帮助我查找解决方案.此处r.readLine()为null
there variable "json" return null.Please help me to find solution.Here r.readLine() is null
推荐答案
您的json输出应为..
Your json output should be ..
{"posts": [{"status": 1,"user_id": 10,"access_token": "BRBL4JJXB9","image": "images/users/.PNG","msg": "Login Successful."}]}
然后您将status
和user_id
解析为string
,但是它是int
,因此您的语法和条件应类似于..
And second you are parsing status
and user_id
as string
but it is int
so your syntax and condition should be like..
int status = jsonObject.getInt("status");
if (status == 1)) {
int userid = jobj.getInt("user_id");
String accesstoken = jobj.getString("access_token");
SharedPreferences.Editor edit = m_pref.edit();
edit.putInt("userid", userid);
edit.putString("accesstoken", accesstoken);
edit.commit();
}
这篇关于JSON解析返回空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文