Android - json返回null

时间:2014-10-22 17:16:50

标签: java android json

我一直试图通过以下链接阅读json:http://echo.jsontest.com/nm_usuario/fulano/us_password/senhadefulano/us_login/fulanologin

但它总是抛出一个异常,所以这里是代码:

 public String getUser() {
        DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
        //http://echo.jsontest.com/nm_usuario/fulano/us_password/senhadefulano/us_login/fulanologin
        HttpPost httppost = new HttpPost("http://echo.jsontest.com/nm_usuario/fulano/us_password/senhadefulano/us_login/fulanologin");

        httppost.setHeader("Content-type", "application/json");

        InputStream inputStream = null;
        String result = null;
        try {
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();

            inputStream = entity.getContent();

            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
                }
            result = sb.toString();

        } catch (Exception e) {
            // Oops
            Log.i("Message:", "falhou");
            e.printStackTrace();
        } finally {
            try {
                Log.i("JSON: ", result);
                if (inputStream != null)
                    inputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

此代码来自其他链接:How to parse JSON in Android

这是logcat:

10-22 15:06:02.958  10706-10706/house_solutions.hoso D/OpenGLRenderer﹕ Enabling debug mode 0
10-22 15:06:05.218  10706-10706/house_solutions.hoso I/Message:﹕ falhou
10-22 15:06:05.223  10706-10706/house_solutions.hoso W/System.err﹕ android.os.NetworkOnMainThreadException
10-22 15:06:05.235  10706-10706/house_solutions.hoso W/System.err﹕ at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
10-22 15:06:05.235  10706-10706/house_solutions.hoso W/System.err﹕ at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
10-22 15:06:05.235  10706-10706/house_solutions.hoso W/System.err﹕ at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-22 15:06:05.235  10706-10706/house_solutions.hoso W/System.err﹕ at java.net.InetAddress.getAllByName(InetAddress.java:214)
10-22 15:06:05.235  10706-10706/house_solutions.hoso W/System.err﹕ at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
10-22 15:06:05.236  10706-10706/house_solutions.hoso W/System.err﹕ at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-22 15:06:05.236  10706-10706/house_solutions.hoso W/System.err﹕ at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-22 15:06:05.236  10706-10706/house_solutions.hoso W/System.err﹕ at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:374)
10-22 15:06:05.236  10706-10706/house_solutions.hoso W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:575)
10-22 15:06:05.237  10706-10706/house_solutions.hoso W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:498)
10-22 15:06:05.237  10706-10706/house_solutions.hoso W/System.err﹕ at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:476)
10-22 15:06:05.237  10706-10706/house_solutions.hoso W/System.err﹕ at house_solutions.hoso.helpers.RegisterJson.getUser(RegisterJson.java:29)
10-22 15:06:05.237  10706-10706/house_solutions.hoso W/System.err﹕ at house_solutions.hoso.Menu.onCreate(Menu.java:33)
10-22 15:06:05.237  10706-10706/house_solutions.hoso W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5248)
10-22 15:06:05.238  10706-10706/house_solutions.hoso W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
10-22 15:06:05.238  10706-10706/house_solutions.hoso W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
10-22 15:06:05.238  10706-10706/house_solutions.hoso W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
10-22 15:06:05.238  10706-10706/house_solutions.hoso W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java:139)
10-22 15:06:05.239  10706-10706/house_solutions.hoso W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
10-22 15:06:05.239  10706-10706/house_solutions.hoso W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
10-22 15:06:05.240  10706-10706/house_solutions.hoso W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
10-22 15:06:05.240  10706-10706/house_solutions.hoso W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5086)
10-22 15:06:05.240  10706-10706/house_solutions.hoso W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
10-22 15:06:05.241  10706-10706/house_solutions.hoso W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
10-22 15:06:05.241  10706-10706/house_solutions.hoso W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-22 15:06:05.241  10706-10706/house_solutions.hoso W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-22 15:06:05.242  10706-10706/house_solutions.hoso W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
10-22 15:06:05.242  10706-10706/house_solutions.hoso W/System.err﹕ java.lang.NullPointerException: println needs a message
10-22 15:06:05.246  10706-10706/house_solutions.hoso W/System.err﹕ at android.util.Log.println_native_inner(Native Method)
10-22 15:06:05.246  10706-10706/house_solutions.hoso W/System.err﹕ at android.util.Log.println_native(Log.java:290)
10-22 15:06:05.247  10706-10706/house_solutions.hoso W/System.err﹕ at android.util.Log.i(Log.java:344)
10-22 15:06:05.247  10706-10706/house_solutions.hoso W/System.err﹕ at house_solutions.hoso.helpers.RegisterJson.getUser(RegisterJson.java:49)
10-22 15:06:05.248  10706-10706/house_solutions.hoso W/System.err﹕ at house_solutions.hoso.Menu.onCreate(Menu.java:33)
10-22 15:06:05.249  10706-10706/house_solutions.hoso W/System.err﹕ at android.app.Activity.performCreate(Activity.java:5248)
10-22 15:06:05.249  10706-10706/house_solutions.hoso W/System.err﹕ at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
10-22 15:06:05.249  10706-10706/house_solutions.hoso W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
10-22 15:06:05.250  10706-10706/house_solutions.hoso W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
10-22 15:06:05.251  10706-10706/house_solutions.hoso W/System.err﹕ at android.app.ActivityThread.access$800(ActivityThread.java:139)
10-22 15:06:05.251  10706-10706/house_solutions.hoso W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
10-22 15:06:05.252  10706-10706/house_solutions.hoso W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
10-22 15:06:05.252  10706-10706/house_solutions.hoso W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
10-22 15:06:05.252  10706-10706/house_solutions.hoso W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5086)
10-22 15:06:05.253  10706-10706/house_solutions.hoso W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
10-22 15:06:05.254  10706-10706/house_solutions.hoso W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
10-22 15:06:05.254  10706-10706/house_solutions.hoso W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-22 15:06:05.255  10706-10706/house_solutions.hoso W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-22 15:06:05.255  10706-10706/house_solutions.hoso W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
10-22 15:06:05.257  10706-10706/house_solutions.hoso D/CheckNetwork﹕ Hapiness!!!
10-22 15:06:05.271  10706-10706/house_solutions.hoso I/Username:﹕ Diego
10-22 15:06:25.405  10706-10706/house_solutions.hoso W/IInputConnectionWrapper﹕ getExtractedText on inactive InputConnection
10-22 15:06:25.406  10706-10706/house_solutions.hoso W/IInputConnectionWrapper﹕ getTextBeforeCursor on inactive InputConnection
10-22 15:06:25.407  10706-10706/house_solutions.hoso W/IInputConnectionWrapper﹕ getSelectedText on inactive InputConnection
10-22 15:06:25.407  10706-10706/house_solutions.hoso W/IInputConnectionWrapper﹕ getTextAfterCursor on inactive InputConnection

所以基本上每次我尝试填充变量"结果"当我尝试记录它时它返回null,并且我不确定它为什么会发生

此外,我已将权限发布到Android清单文件中。

感谢。

0 个答案:

没有答案