android编程中的JsonException

时间:2013-08-14 13:30:44

标签: android json http-post

当我通过http post获取参数时,我收到了JsonException

以下是代码:

protected ArrayList<String> doInBackground(ArrayList<String>... params) {

        ArrayList<String> nameSuccess = new ArrayList<String>();
        HttpClient httpclient = new DefaultHttpClient();
        GeneralConstans GC = new GeneralConstans();
        HttpPost httpget = new HttpPost(GC.PasswordUrl);
        HttpResponse response;
        String result = null;
        try {

            HttpContext ctx = new BasicHttpContext();

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                    2);
            nameValuePairs.add(new BasicNameValuePair("otpPwd", EdtText
                    .getText().toString()));
            httpget.setEntity(new UrlEncodedFormEntity(nameValuePairs,
                    "UTF-8"));

            response = httpclient.execute(httpget, ctx);
            HttpEntity resEntity = response.getEntity();

            if (resEntity != null) {
                result = EntityUtils.toString(resEntity);
                JSONObject arr = new JSONObject(result);

                name = (arr.get("name")).toString();
                error = (arr.get("error")).toString();
                nameSuccess.add(0, name);
                nameSuccess.add(1, error);
            }
            return nameSuccess;


        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

JSONObject arr = new JSONObject(result);行我得到了例外。

我是Android编程新手。

loccat: 08-14 16:23:59.388:E / WindowManager(10626):活动tr.com.turkcell.shmobile.PasswordConfirmationActivity泄露了最初添加的窗口com.android.internal.policy.impl.PhoneWindow$DecorView@414fa9e0 08-14 16:23:59.388:E / WindowManager(10626):android.view.WindowLeaked:Activity tr.com.turkcell.shmobile.PasswordConfirmationActivity已泄露窗口com.android.internal.policy.impl.PhoneWindow$DecorView@414fa9e0这最初是在这里添加的 08-14 16:23:59.388:E / WindowManager(10626):在android.view.ViewRootImpl。(ViewRootImpl.java:374) 08-14 16:23:59.388:E / WindowManager(10626):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292) 08-14 16:23:59.388:E / WindowManager(10626):在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224) 08-14 16:23:59.388:E / WindowManager(10626):在android.view.WindowManagerImpl $ CompatModeWrapper.addView(WindowManagerImpl.java:149) 08-14 16:23:59.388:E / WindowManager(10626):在android.view.Window $ LocalWindowManager.addView(Window.java:549) 08-14 16:23:59.388:E / WindowManager(10626):在android.app.Dialog.show(Dialog.java:277) 08-14 16:23:59.388:E / WindowManager(10626):在android.app.ProgressDialog.show(ProgressDialog.java:116) 08-14 16:23:59.388:E / WindowManager(10626):在android.app.ProgressDialog.show(ProgressDialog.java:99) 08-14 16:23:59.388:E / WindowManager(10626):在android.app.ProgressDialog.show(ProgressDialog.java:94) 08-14 16:23:59.388:E / WindowManager(10626):at tr.com.turkcell.shmobile.PasswordConfirmationActivity $ GetName.onPreExecute(PasswordConfirmationActivity.java:140) 08-14 16:23:59.388:E / WindowManager(10626):在android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586) 08-14 16:23:59.388:E / WindowManager(10626):在android.os.AsyncTask.execute(AsyncTask.java:534) 08-14 16:23:59.388:E / WindowManager(10626):at tr.com.turkcell.shmobile.PasswordConfirmationActivity $ 1 $ 1.run(PasswordConfirmationActivity.java:53) 08-14 16:23:59.388:E / WindowManager(10626):在android.app.Activity.runOnUiThread(Activity.java:4591) 08-14 16:23:59.388:E / WindowManager(10626):at tr.com.turkcell.shmobile.PasswordConfirmationActivity $ 1.onClick(PasswordConfirmationActivity.java:50) 08-14 16:23:59.388:E / WindowManager(10626):在android.view.View.performClick(View.java:4084) 08-14 16:23:59.388:E / WindowManager(10626):在android.view.View $ PerformClick.run(View.java:16966) 08-14 16:23:59.388:E / WindowManager(10626):在android.os.Handler.handleCallback(Handler.java:615) 08-14 16:23:59.388:E / WindowManager(10626):在android.os.Handler.dispatchMessage(Handler.java:92) 08-14 16:23:59.388:E / WindowManager(10626):在android.os.Looper.loop(Looper.java:137) 08-14 16:23:59.388:E / WindowManager(10626):在android.app.ActivityThread.main(ActivityThread.java:4754) 08-14 16:23:59.388:E / WindowManager(10626):at java.lang.reflect.Method.invokeNative(Native Method) 08-14 16:23:59.388:E / WindowManager(10626):at java.lang.reflect.Method.invoke(Method.java:511) 08-14 16:23:59.388:E / WindowManager(10626):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:833) 08-14 16:23:59.388:E / WindowManager(10626):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 08-14 16:23:59.388:E / WindowManager(10626):at dalvik.system.NativeStart.main(Native Method) 08-14 16:24:00.788:W / System.err(10626):org.json.JSONException:Value(JSONObject.java:158) 08-14 16:24:00.788:W / System.err(10626):at org.json.JSONObject。(JSONObject.java:171) 08-14 16:24:00.788:W / System.err(10626):at tr.com.turkcell.shmobile.PasswordConfirmationActivity $ GetName.doInBackground(PasswordConfirmationActivity.java:102) 08-14 16:24:00.798:W / System.err(10626):at tr.com.turkcell.shmobile.PasswordConfirmationActivity $ GetName.doInBackground(PasswordConfirmationActivity.java:1) 08-14 16:24:00.798:W / System.err(10626):在android.os.AsyncTask $ 2.call(AsyncTask.java:287) 08-14 16:24:00.798:W / System.err(10626):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:305) 08-14 16:24:00.798:W / System.err(10626):at java.util.concurrent.FutureTask.run(FutureTask.java:137) 08-14 16:24:00.798:W / System.err(10626):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230) 08-14 16:24:00.798:W / System.err(10626):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 08-14 16:24:00.798:W / System.err(10626):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:569)

1 个答案:

答案 0 :(得分:0)

尝试使用以下代码使用HTTP Post方法连接到您的网络服务器:

    HttpPost post = new HttpPost("SERVER_URL");
    HttpClient hc = new DefaultHttpClient();
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("otpPwd", EdtText
                .getText().toString()));
    try {
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        post.getParams().setBooleanParameter("http.protocol.expect-continue", false);
        HttpResponse rp = hc.execute(post);
        json = EntityUtils.toString(rp.getEntity());
        if (!json.equals("") || !json.equals(null)) {
            JSONObject arr = new JSONObject(result);
            name = (arr.get("name")).toString();
            error = (arr.get("error")).toString();
            nameSuccess.add(0, name);
            nameSuccess.add(1, error);
        }
        return json;
    } catch (NoRouteToHostException er) {
        er.printStackTrace();
        return null;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }