无法解析简单的json字符串(android)

时间:2015-05-06 12:17:15

标签: java php android json

抱歉我的英文。我无法将json发送到服务器。我的算法:用户检查来自图库的图像,然后写下关于你自己的所有信息(姓名,电子邮件),然后点击插入服务器并添加数据是 - > 第一次加载图片及其返回的json字符串int我解析这个json字符串是这样的:

{"status":"success","url":"\/files\/clients_photos\/tmp\/snapshot_tmp.jpg"}

和变量JsonObject jobj = new Gson().fromJson(imageUrl, JsonObject.class); myRes = jobj.get("url").getAsString(); 使用所有数据json发布到服务器。我认为这是错误,但我可以解决它。以下是我的错误:

myRes

以及我的代码:

   05-06 15:06:46.621: E/AndroidRuntime(21766): FATAL EXCEPTION: AsyncTask #1
        05-06 15:06:46.621: E/AndroidRuntime(21766): java.lang.RuntimeException: An error occured while executing doInBackground()
        05-06 15:06:46.621: E/AndroidRuntime(21766):    at android.os.AsyncTask$3.done(AsyncTask.java:278)
        05-06 15:06:46.621: E/AndroidRuntime(21766):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
        05-06 15:06:46.621: E/AndroidRuntime(21766):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
        05-06 15:06:46.621: E/AndroidRuntime(21766):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
        05-06 15:06:46.621: E/AndroidRuntime(21766):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        05-06 15:06:46.621: E/AndroidRuntime(21766):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
        05-06 15:06:46.621: E/AndroidRuntime(21766):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        05-06 15:06:46.621: E/AndroidRuntime(21766):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        05-06 15:06:46.621: E/AndroidRuntime(21766):    at java.lang.Thread.run(Thread.java:856)
        05-06 15:06:46.621: E/AndroidRuntime(21766): Caused by: java.lang.NullPointerException
        05-06 15:06:46.621: E/AndroidRuntime(21766):    at profile.Registration$GetPollsInformation.doInBackground(Registration.java:179)
        05-06 15:06:46.621: E/AndroidRuntime(21766):    at profile.Registration$GetPollsInformation.doInBackground(Registration.java:1)
        05-06 15:06:46.621: E/AndroidRuntime(21766):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
        05-06 15:06:46.621: E/AndroidRuntime(21766):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
        05-06 15:06:46.621: E/AndroidRuntime(21766):    ... 5 more

05-06 15:22:47.946: E/WindowManager(22921): Activity profile.Registration has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@419acd68 that was originally added here
05-06 15:22:47.946: E/WindowManager(22921): android.view.WindowLeaked: Activity profile.Registration has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@419acd68 that was originally added here
05-06 15:22:47.946: E/WindowManager(22921):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:363)
05-06 15:22:47.946: E/WindowManager(22921):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:268)
05-06 15:22:47.946: E/WindowManager(22921):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:216)
05-06 15:22:47.946: E/WindowManager(22921):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:141)
05-06 15:22:47.946: E/WindowManager(22921):     at android.view.Window$LocalWindowManager.addView(Window.java:537)
05-06 15:22:47.946: E/WindowManager(22921):     at android.app.Dialog.show(Dialog.java:278)
05-06 15:22:47.946: E/WindowManager(22921):     at profile.Registration$GetPollsInformation.onPreExecute(Registration.java:146)
05-06 15:22:47.946: E/WindowManager(22921):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
05-06 15:22:47.946: E/WindowManager(22921):     at android.os.AsyncTask.execute(AsyncTask.java:511)
05-06 15:22:47.946: E/WindowManager(22921):     at profile.Registration$1.onClick(Registration.java:74)
05-06 15:22:47.946: E/WindowManager(22921):     at android.view.View.performClick(View.java:3517)
05-06 15:22:47.946: E/WindowManager(22921):     at android.view.View$PerformClick.run(View.java:14155)
05-06 15:22:47.946: E/WindowManager(22921):     at android.os.Handler.handleCallback(Handler.java:605)
05-06 15:22:47.946: E/WindowManager(22921):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-06 15:22:47.946: E/WindowManager(22921):     at android.os.Looper.loop(Looper.java:154)
05-06 15:22:47.946: E/WindowManager(22921):     at android.app.ActivityThread.main(ActivityThread.java:4624)
05-06 15:22:47.946: E/WindowManager(22921):     at java.lang.reflect.Method.invokeNative(Native Method)
05-06 15:22:47.946: E/WindowManager(22921):     at java.lang.reflect.Method.invoke(Method.java:511)
05-06 15:22:47.946: E/WindowManager(22921):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
05-06 15:22:47.946: E/WindowManager(22921):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
05-06 15:22:47.946: E/WindowManager(22921):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

Gson pojo用于解析

class QueryBuilder {
  _db$: Observable;
  _sub: Disposable;
  _selectors: Object;

  constructor(db$: Observable, selectors?: Object) {
    this._db$ = db$;

    this._sub = db$.subscribe(
      _ => {
        if (__DEV__) {
          console.log('Connected to database on', url);
        }
      },
      err => console.log('Database connection error:', err.message, err.stack)
    );

    if (!selectors) {
      this._selectors = assign({
        collection: null,
        query: {},
        opts: {},
        sort: {},
        offset: 0,
        limit: 0
      }, selectors);
    }
    else {
      this._selectors = selectors;
    }
  }

  static connect(url: string): QueryBuilder {
    var connect = Observable.fromNodeCallback(client.connect, client);
    var db$ = connect(db.url);

    return new QueryBuilder(db$);
  }

  close(): any {
    this._sub.dispose();

    return QueryBuilder;
  }
}

解析回复

public class RequestResponse {

private String status;
private String url;

//getter and setter goes here
}