Java 如何解析json分支值
我对android编程还不熟悉。我曾试图通过解析它来让toast在我的android应用程序中显示用户id的值,但错误显示没有用户id的值。如何在android中从json数据获取输入?我在下面附上了我的logcat错误。有人能帮我吗?提前谢谢Java 如何解析json分支值,java,android,json,eclipse,android-emulator,Java,Android,Json,Eclipse,Android Emulator,我对android编程还不熟悉。我曾试图通过解析它来让toast在我的android应用程序中显示用户id的值,但错误显示没有用户id的值。如何在android中从json数据获取输入?我在下面附上了我的logcat错误。有人能帮我吗?提前谢谢 "status": "SUCCESS", "msg": "Login Successful", "data": { "user": { "id": "6", "first_name": "e", "
"status": "SUCCESS",
"msg": "Login Successful",
"data": {
"user": {
"id": "6",
"first_name": "e",
"last_name": "ff",
"address": "dr",
"mobile": "55",
"email": "rokes1990@live.com",
"password": "4124bc0a9335c27f086f24ba207a4912",
"pwd_reset_key": "",
"name_on_card": "",
"card_number": "",
"expiry_date": "",
"cvv": "",
"tdatetime": "2014-07-11 02:55:08"
}
}
LogCat错误:
09-05 03:36:00.567: D/dalvikvm(2227): GC_FOR_ALLOC freed 379K, 8% free 5765K/6220K, paused 76ms, total 82ms
09-05 03:36:02.077: W/Response(2227): {"GET":[],"POST":{"action":"login","app_secret":"jkhljkUILJGJkljhkjUGLG87796587687HGKJhghkjKUYGKJHjhgjUYGKUY7865876hgKUYGK","email":"rokes1990@live.com","password":"aa"},"status":"SUCCESS","msg":"Login Successful","data":{"user":{"id":"6","first_name":"e","last_name":"ff","address":"dr","mobile":"55","email":"rokes1990@live.com","password":"4124bc0a9335c27f086f24ba207a4912","pwd_reset_key":"","name_on_card":"","card_number":"","expiry_date":"","cvv":"","tdatetime":"2014-07-11 02:55:08"}}}
09-05 03:36:02.107: W/System.err(2227): org.json.JSONException: No value for user
09-05 03:36:02.117: W/System.err(2227): at org.json.JSONObject.get(JSONObject.java:355)
09-05 03:36:02.117: W/System.err(2227): at org.json.JSONObject.getString(JSONObject.java:515)
09-05 03:36:02.147: W/System.err(2227): at example.atlcitylimo.Main.postLoginData(Main.java:116)
09-05 03:36:02.147: W/System.err(2227): at example.atlcitylimo.Main.onClick(Main.java:181)
09-05 03:36:02.147: W/System.err(2227): at android.view.View.performClick(View.java:4438)
09-05 03:36:02.147: W/System.err(2227): at android.view.View$PerformClick.run(View.java:18422)
09-05 03:36:02.147: W/System.err(2227): at android.os.Handler.handleCallback(Handler.java:733)
09-05 03:36:02.147: W/System.err(2227): at android.os.Handler.dispatchMessage(Handler.java:95)
09-05 03:36:02.147: W/System.err(2227): at android.os.Looper.loop(Looper.java:136)
09-05 03:36:02.157: W/System.err(2227): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-05 03:36:02.157: W/System.err(2227): at java.lang.reflect.Method.invokeNative(Native Method)
09-05 03:36:02.157: W/System.err(2227): at java.lang.reflect.Method.invoke(Method.java:515)
09-05 03:36:02.157: W/System.err(2227): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-05 03:36:02.157: W/System.err(2227): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-05 03:36:02.157: W/System.err(2227): at dalvik.system.NativeStart.main(Native Method)
您可以使用截取来解析json字符串。选中以下链接中的“发出JSON请求”: Volley提供了一种易于生成json请求的方法。如果响应中需要json对象,则应使用
JsonObjectRequest
类,或者如果响应是json数组,则应使用JsonArrayRequest
类<代码>字符串请求类将用于获取任何类型的字符串数据。响应可以是json、xml、html或纯文本
您可以从JSONObject请求开始获取JSONObject:
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
//response is the JSONObject that you get from the request
//you can play with that request to get the exact info you want
String status = response.getString("status");
String msg = response.getString("msg");
JSONObject data = null;
try{
data = response.getJSONObject("data");
}catch (JSONException e1) {
e1.printStackTrace();
}
JSONObject user = null;
try{
user = data.getJSONObject("user");
}catch (JSONException e1) {
e1.printStackTrace();
}
String id = user.getString("id");
String first_name = user.getString("first_name");
//and goes on for the rest of the user info
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
}
});
您的AppController类如下所示:
import android.app.Application;
import android.text.TextUtils;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
public class AppController extends Application {
public static final String TAG = AppController.class
.getSimpleName();
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public ImageLoader getImageLoader() {
getRequestQueue();
if (mImageLoader == null) {
mImageLoader = new ImageLoader(this.mRequestQueue,
new LruBitmapCache());
}
return this.mImageLoader;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
导入android.app.Application;
导入android.text.TextUtils;
导入com.android.volley.Request;
导入com.android.volley.RequestQueue;
导入com.android.volley.toolbox.ImageLoader;
导入com.android.volley.toolbox.volley;
公共类AppController扩展应用程序{
公共静态最终字符串标记=AppController.class
.getSimpleName();
私有请求队列mRequestQueue;
私有图像加载器;
专用静态应用控制器;
@凌驾
public void onCreate(){
super.onCreate();
mInstance=这个;
}
公共静态同步AppController getInstance(){
回报率;
}
公共请求队列getRequestQueue(){
if(mRequestQueue==null){
mRequestQueue=Volley.newRequestQueue(getApplicationContext());
}
返回mrequest队列;
}
公共ImageLoader getImageLoader(){
getRequestQueue();
if(mImageLoader==null){
mImageLoader=新的ImageLoader(this.mRequestQueue,
新的LruBitmapCache());
}
返回此.mImageLoader;
}
公共无效addToRequestQueue(请求请求,字符串标记){
//如果标记为空,则设置默认标记
请求setTag(TextUtils.isEmpty(tag)?tag:tag;
getRequestQueue().add(请求);
}
公共无效addToRequestQueue(请求请求){
要求设置标签(标签);
getRequestQueue().add(请求);
}
公共作废取消挂起请求(对象标记){
if(mRequestQueue!=null){
mRequestQueue.cancelAll(标记);
}
}
}
最后在Manifest.xml中添加
标记
<application
android:name="info.androidhive.volleyexamples.app.AppController"
@mystic_knight,欢迎您的光临,但别忘了接受答案
import android.app.Application;
import android.text.TextUtils;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
public class AppController extends Application {
public static final String TAG = AppController.class
.getSimpleName();
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public ImageLoader getImageLoader() {
getRequestQueue();
if (mImageLoader == null) {
mImageLoader = new ImageLoader(this.mRequestQueue,
new LruBitmapCache());
}
return this.mImageLoader;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
<application
android:name="info.androidhive.volleyexamples.app.AppController"