Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSON:从用户定义JSON函数获取空字符串_Java_Android_Json_Eclipse - Fatal编程技术网

Java JSON:从用户定义JSON函数获取空字符串

Java JSON:从用户定义JSON函数获取空字符串,java,android,json,eclipse,Java,Android,Json,Eclipse,我第一次从Json函数中得到null字符串 我有两个类文件: func.java main.java 在func.java中code是 package com.example.json; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java

我第一次从
Json
函数中得到
null
字符串 我有两个类文件:

  • func.java
  • main.java
  • func.java中
    code是

    package com.example.json;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Random;
    import android.content.Context;
    import org.apache.http.Header;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import com.loopj.android.http.AsyncHttpClient;
    import com.loopj.android.http.AsyncHttpResponseHandler;
    
    import android.widget.Toast;
    
    public class func {
        public static String res = null;
    
        public static String jsonv(String url, Context c) {
            AsyncHttpClient client = new AsyncHttpClient();
    
            client.get(url, new AsyncHttpResponseHandler() {
    
                @Override
                public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
    
                    try {
                        res = new String(arg2);
                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        res = "error 1 ";
                    }
    
                    // Toast.makeText(getApplicationContext(), res,
                    // Toast.LENGTH_LONG).show();
                }
    
                @Override
                public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
                    res = "error 2";
                }
            });
            return res;
        }
    }
    
    func.java中,我编写了获取JSON字符串的代码,并且只希望它出现在函数中 然后在main.java中

    public class main extends Activity {
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            String getj = func.jsonv("192.168.71.1/json?app=1", getApplicationContext());
            Toast.makeText(getApplicationContext(), "json is" + getj, 1000).show();
        }
    }
    
    它给出了
    null
    ,因为在应用程序启动时
    json
    仍在加载,所以当
    json
    成功加载时如何执行操作,我只希望它以函数形式出现,这样我就可以随时随地使用它

    这是日志

    06-21 14:10:01.193: I/BufferQueue(19206): [unnamed-19206-0](this:0x60e29008,id:0,api:0,p:-1,c:-1) BufferQueue
    06-21 14:10:01.193: I/GLConsumer(19206): [void android::RingBuffer<TYPE>::resize(uint32_t, bool) [with TYPE = android::sp<android::BackupBuffer>; uint32_t = unsigned int]] 0x60e25e3c resize to 10 (force=true)
    06-21 14:10:01.193: I/GLConsumer(19206): [void android::RingBuffer<TYPE>::resize(uint32_t, bool) [with TYPE = android::sp<android::BackupBuffer>; uint32_t = unsigned int]] 0x60e25e3c resize to 0 (force=false)
    06-21 14:10:01.194: I/BufferQueue(19206): [unnamed-19206-0](this:0x60e29008,id:0,api:0,p:-1,c:19206) consumerConnect consumer=(19206:com.example.json) controlledByApp=true
    06-21 14:10:01.194: I/BufferQueue(19206): [unnamed-19206-0](this:0x60e29008,id:0,api:0,p:-1,c:19206) setConsumerName: unnamed-19206-0
    06-21 14:10:01.194: I/GLConsumer(19206): [unnamed-19206-0](this:0x60e29670,api:0) GLConsumer
    06-21 14:10:01.194: I/GLConsumer(19206): [unnamed-19206-0](this:0x60e29670,api:0) detachFromContext
    06-21 14:10:01.204: D/VelocityTracker(19206): Couldn't open '/dev/touch' (Permission denied)
    06-21 14:10:01.204: D/VelocityTracker(19206): tpd read x fail: Bad file number
    06-21 14:10:01.205: D/VelocityTracker(19206): tpd read y fail: Bad file number
    06-21 14:10:01.205: D/WebViewCallback(19206): getVisitedHistory
    06-21 14:10:01.219: D/dalvikvm(19206): threadid=15: interp stack at 0x6163b000
    06-21 14:10:01.222: D/MediatekClassFactory(19206): createInstance(): Begin = 5611917
    06-21 14:10:01.222: D/MediatekClassFactory(19206): create Instance with :  interface com.mediatek.common.telephony.IOnlyOwnerSimSupport
    06-21 14:10:01.222: W/MediatekClassFactory(19206): Tablet not exist!, Get obj from default class
    06-21 14:10:01.223: D/MediatekClassFactory(19206): create Instance from tablet library :  com.mediatek.tb.telephony.OnlyOwnerSimSupport
    06-21 14:10:01.223: D/MediatekClassFactory(19206): createInstance(): End = 5611918
    06-21 14:10:01.303: D/dalvikvm(19206): create interp thread : stack size=128KB
    06-21 14:10:01.303: D/dalvikvm(19206): create new thread
    06-21 14:10:01.304: D/dalvikvm(19206): new thread created
    06-21 14:10:01.304: D/dalvikvm(19206): update thread list
    06-21 14:10:01.304: D/dalvikvm(19206): threadid=16: interp stack at 0x61694000
    06-21 14:10:01.304: D/dalvikvm(19206): threadid=16: created from interp
    06-21 14:10:01.304: D/dalvikvm(19206): start new thread
    06-21 14:10:01.304: D/dalvikvm(19206): threadid=16: notify debugger
    06-21 14:10:01.304: D/dalvikvm(19206): threadid=16 (RefQueueWorker@org.apache.http.impl.conn.tsccm.ConnPoolByRoute@427f7c80): calling run()
    06-21 14:10:01.309: I/System.out(19206): httpget:http://192.168.71.1/json/?app=0b52765ac1d697a37891605df3c27e0f
    06-21 14:10:01.310: I/System.out(19206): http://192.168.71.1/json/?app=0b52765ac1d697a37891605df3c27e0f
    06-21 14:10:01.310: D/dalvikvm(19206): create interp thread : stack size=128KB
    06-21 14:10:01.310: D/dalvikvm(19206): create new thread
    06-21 14:10:01.310: D/dalvikvm(19206): new thread created
    06-21 14:10:01.310: D/dalvikvm(19206): update thread list
    06-21 14:10:01.311: D/dalvikvm(19206): threadid=17: interp stack at 0x616b9000
    06-21 14:10:01.311: D/dalvikvm(19206): threadid=17: created from interp
    06-21 14:10:01.311: D/dalvikvm(19206): start new thread
    06-21 14:10:01.311: D/dalvikvm(19206): threadid=17: notify debugger
    06-21 14:10:01.311: D/dalvikvm(19206): threadid=17 (pool-1-thread-1): calling run()
    06-21 14:10:01.317: I/System.out(19206): [socket][0] connection /192.168.71.1:80;LocalPort=56751(10000)
    06-21 14:10:01.317: I/System.out(19206): [CDS]connect[/192.168.71.1:80] tm:10
    06-21 14:10:01.317: D/Posix(19206): [Posix_connect Debug]Process com.example.json :80 
    06-21 14:10:01.318: I/System.out(19206): [socket][/192.168.71.101:56751] connected
    06-21 14:10:01.318: I/System.out(19206): [CDS]rx timeout:10000
    06-21 14:10:01.319: I/System.out(19206): [CDS]SO_SND_TIMEOUT:0
    06-21 14:10:01.320: D/ActivityThread(19206): ACT-AM_ON_RESUME_CALLED ActivityRecord{41f53428 token=android.os.BinderProxy@41f52b48 {com.example.json/com.example.json.MainActivity}}
    06-21 14:10:01.321: I/System.out(19206): >doSendRequest
    06-21 14:10:01.323: I/System.out(19206): <doSendRequest
    06-21 14:10:01.323: V/PhoneWindow(19206): DecorView setVisiblity: visibility = 4 ,Parent =null, this =com.android.internal.policy.impl.PhoneWindow$DecorView{41f7b548 I.E..... R.....ID 0,0-0,0}
    06-21 14:10:01.333: V/PhoneWindow(19206): DecorView setVisiblity: visibility = 0 ,Parent =ViewRoot{42821dd0 com.example.json/com.example.json.MainActivity,ident = 0}, this =com.android.internal.policy.impl.PhoneWindow$DecorView{41f7b548 V.E..... R.....ID 0,0-0,0}
    06-21 14:10:01.333: D/ActivityThread(19206): ACT-LAUNCH_ACTIVITY handled : 0 / ActivityRecord{41f53428 token=android.os.BinderProxy@41f52b48 {com.example.json/com.example.json.MainActivity}}
    06-21 14:10:01.371: E/(19206): appName=com.example.json, acAppName=/system/bin/surfaceflinger
    06-21 14:10:01.371: E/(19206): 0
    06-21 14:10:01.374: D/GraphicBuffer(19206): create handle(0x617e0348) (w:544, h:960, f:1)
    06-21 14:10:01.378: I/MaliEGL(19206): [Mali]window_type=1, is_framebuffer=0, errnum = 0
    06-21 14:10:01.378: I/MaliEGL(19206): [Mali]surface->num_buffers=4, surface->num_frames=3, win_min_undequeued=1
    06-21 14:10:01.378: I/MaliEGL(19206): [Mali]max_allowed_dequeued_buffers=3
    06-21 14:10:01.378: D/GraphicBuffer(19206): close handle(0x617e0348) (w:544 h:960 f:1)
    06-21 14:10:01.380: I/(19206): [custom_prop]custom_get_string->module[browser],key[UAProfileURL]
    06-21 14:10:01.380: I/(19206): [custom_prop]reset_parser->keep[0]
    06-21 14:10:01.380: I/(19206): [custom_prop]enter STATE_VALUE
    06-21 14:10:01.380: I/(19206): [custom_prop]reset_parser->keep[1]
    06-21 14:10:01.380: D/GraphicBuffer(19206): create handle(0x617e2de8) (w:544, h:960, f:1)
    06-21 14:10:01.382: D/OpenGLRenderer(19206): Enabling debug mode 0
    06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_VALUE
    06-21 14:10:01.382: I/(19206): [custom_prop]reset_parser->keep[1]
    06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_VALUE
    06-21 14:10:01.382: I/(19206): [custom_prop]reset_parser->keep[1]
    06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_VALUE
    06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_PROP
    06-21 14:10:01.382: I/(19206): [custom_prop]prop name:ro.build.version.release
    06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_VALUE
    06-21 14:10:01.382: I/(19206): [custom_prop]reset_parser->keep[1]
    06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_VALUE
    06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_PROP
    06-21 14:10:01.382: I/(19206): [custom_prop]prop name:ro.build.version.release
    06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_VALUE
    06-21 14:10:01.382: I/(19206): [custom_prop]reset_parser->keep[1]
    06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_VALUE
    06-21 14:10:01.382: I/(19206): [custom_prop]reset_parser->keep[1]
    06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_VALUE
    06-21 14:10:01.382: I/(19206): [custom_prop]reset_parser->keep[1]
    06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_VALUE
    06-21 14:10:01.382: I/(19206): [custom_prop]reset_parser->keep[1]
    06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_VALUE
    06-21 14:10:01.382: I/(19206): [custom_prop]reset_parser->keep[1]
    06-21 14:10:01.382: I/(19206): [custom_prop]enter STATE_VALUE
    06-21 14:10:01.382: I/(19206): [custom_prop]reset_parser->keep[1]
    06-21 14:10:01.382: I/(19206): [custom_prop]reset_parser->keep[0]
    06-21 14:10:01.383: D/GraphicBuffer(19206): create handle(0x617e1968) (w:768, h:768, f:1)
    06-21 14:10:01.385: I/[MALI][Gralloc](19206): dlopen libsec_mem.so fail
    06-21 14:10:01.386: D/OpenGLRenderer(19206): setViewport 540x960 <0x60e3c360>
    06-21 14:10:01.387: I/BufferQueue(19206): [unnamed-19206-0](this:0x60e29008,id:0,api:0,p:-1,c:19206) setDefaultBufferSize: w=150, h=150
    06-21 14:10:01.387: I/BufferQueue(19206): [unnamed-19206-0](this:0x60e29008,id:0,api:0,p:-1,c:19206) setDefaultBufferSize: w=150, h=150
    06-21 14:10:01.403: V/InputMethodManager(19206): onWindowFocus: android.webkit.WebView{41f76e28 VFED..C. .F....ID 195,488-345,638 #7f0a0003 app:id/webView1} softInputMode=288 first=true flags=#1810100
    06-21 14:10:01.404: V/InputMethodManager(19206): START INPUT: android.webkit.WebView{41f76e28 VFED..C. .F....ID 195,488-345,638 #7f0a0003 app:id/webView1} ic=com.android.org.chromium.content.browser.input.AdapterInputConnection@42835280 tba=android.view.inputmethod.EditorInfo@42835190 controlFlags=#105
    06-21 14:10:01.415: W/AwContents(19206): nativeOnDraw failed; clearing to background color.
    06-21 14:10:01.416: D/OpenGLRenderer(19206): prepareDirty (0.00, 0.00, 540.00, 960.00) opaque 1 <0x60e3c360>
    06-21 14:10:01.463: D/OpenGLRenderer(19206): finish <0x60e3c360>
    06-21 14:10:01.468: D/WebViewCallback(19206): onProgressChanged=0
    06-21 14:10:01.471: V/AsyncHttpResponseHandler(19206): Progress 90 from 90 
    
    06-21 14:10:01.193:I/BufferQueue(19206):[unnamed-19206-0](此代码:0x60e29008,id:0,api:0,p:-1,c:-1)BufferQueue
    I/GLConsumer(19206):[void android::RingBuffer::resize(uint32_t,bool)[with TYPE=android::sp;uint32_t=unsigned int]]0x60e25e3c将大小调整为10(force=true)
    I/GLConsumer(19206):[void android::RingBuffer::resize(uint32_t,bool)[with TYPE=android::sp;uint32_t=unsigned int]]0x60e25e3c将大小调整为0(force=false)
    06-21 14:10:01.194:I/BufferQueue(19206):[unnamed-19206-0](this:0x60e29008,id:0,api:0,p:-1,c:19206)consumerConnect consumer=(19206:com.example.json)controlledByApp=true
    06-21 14:10:01.194:I/BufferQueue(19206):[unnamed-19206-0](此代码:0x60e29008,id:0,api:0,p:-1,c:19206)setConsumerName:unnamed-19206-0
    06-21 14:10:01.194:I/GLConsumer(19206):[unnamed-19206-0](此代码:0x60e29670,api:0)GLConsumer
    06-21 14:10:01.194:I/GLConsumer(19206):[unnamed-19206-0](此代码:0x60e29670,api:0)脱离上下文
    06-21 14:10:01.204:D/VelocityTracker(19206):无法打开“/dev/touch”(权限被拒绝)
    06-21 14:10:01.204:D/VelocityTracker(19206):tpd读取x失败:错误的文件号
    06-21 14:10:01.205:D/VelocityTracker(19206):tpd读取y失败:错误的文件号
    06-21 14:10:01.205:D/WebViewCallback(19206):getVisitedHistory
    06-21 14:10:01.219:D/dalvikvm(19206):threadid=15:0x6163b000处的内部堆栈
    06-21 14:10:01.222:D/MediatekClassFactory(19206):createInstance():Begin=5611917
    06-21 14:10:01.222:D/MediatekClassFactory(19206):使用接口com.mediatek.common.telephony.IOnlyOwnerSimSupport创建实例
    06-21 14:10:01.222:W/MediatekClassFactory(19206):平板电脑不存在!,从默认类获取obj
    06-21 14:10:01.223:D/MediatekClassFactory(19206):从平板电脑库创建实例:com.mediatek.tb.telephony.OnlyOwnerSimSupport
    06-21 14:10:01.223:D/MediatekClassFactory(19206):createInstance():End=5611918
    06-21 14:10:01.303:D/dalvikvm(19206):创建内部线程:堆栈大小=128KB
    06-21 14:10:01.303:D/dalvikvm(19206):创建新线程
    06-21 14:10:01.304:D/dalvikvm(19206):创建新线程
    06-21 14:10:01.304:D/dalvikvm(19206):更新线程列表
    06-21 14:10:01.304:D/dalvikvm(19206):threadid=16:0x61694000处的内部堆栈
    06-21 14:10:01.304:D/dalvikvm(19206):threadid=16:从interp创建
    06-21 14:10:01.304:D/dalvikvm(19206):启动新线程
    06-21 14:10:01.304:D/dalvikvm(19206):threadid=16:通知调试器
    06-21 14:10:01.304:D/dalvikvm(19206):threadid=16(RefQueueWorker@org.apache.http.impl.conn.tsccm.ConnPoolByRoute@427f7c80):调用run()
    06-21 14:10:01.309:I/System.out(19206):httpget:http://192.168.71.1/json/?app=0b52765ac1d697a37891605df3c27e0f
    06-21 14:10:01.310:I/系统输出(19206):http://192.168.71.1/json/?app=0b52765ac1d697a37891605df3c27e0f
    06-21 14:10:01.310:D/dalvikvm(19206):创建内部线程:堆栈大小=128KB
    06-21 14:10:01.310:D/dalvikvm(19206):创建新线程
    06-21 14:10:01.310:D/dalvikvm(19206):创建新线程
    06-21 14:10:01.310:D/dalvikvm(19206):更新线程列表
    06-21 14:10:01.311:D/dalvikvm(19206):threadid=17:0x616b9000处的内部堆栈
    06-21 14:10:01.311:D/dalvikvm(19206):threadid=17:从interp创建
    06-21 14:10:01.311:D/dalvikvm(19206):启动新线程
    06-21 14:10:01.311:D/dalvikvm(19206):threadid=17:通知调试器
    06-21 14:10:01.311:D/dalvikvm(19206):threadid=17(pool-1-thread-1):调用run()
    06-21 14:10:01.317:I/System.out(19206):[socket][0]连接/192.168.71.1:80;LocalPort=56751(10000)
    06-21 14:10:01.317:I/System.out(19206):[CDS]连接[/192.168.71.1:80]tm:10
    06-21 14:10:01.317:D/Posix(19206):[Posix_connect Debug]Process com.example.json:80
    06-21 14:10:01.318:I/System.out(19206):[socket][/192.168.71.101:56751]已连接
    06-21 14:10:01.318:I/System.out(19206):[CDS]接收超时:10000
    06-21 14:10:01.319:I/System.out(19206):[CDS]因此超时:0
    06-21 14:10:01.320:D/ActivityThread(19206):ACT-AM_ON__RESUME_称为ActivityRecord{41f53428 token=android.os。BinderProxy@41f52b48{com.example.json/com.example.json.MainActivity}
    06-21 14:10:01.321:I/System.out(19206):>doSendRequest
    06-21 14:10:01.323:I/System.out(19206):num\u buffers=4,surface->num\u frames=3,win\u min\u undequeued=1
    06-21 14:10:01.378:I/MaliEGL(19206):[马里]允许的最大缓冲区数=3
    06-21 14:10:01.378:D/GraphicBuffer(19206):关闭手柄(0x617e0348)(w:544 h:960 f:1)
    06-21 14:10:01.380:I/(19206):[custom_prop]custom_get_string->module[browser],key[UAProfileURL]
    06-21 14:10:01.380:I/(19206):[自定义属性]重置解析器->保留[0]
    06-21 14:10:01.380:I/(19206):[自定义属性]输入状态值
    06-21 14:10:01.380:I/(19206):[自定义属性]重置解析器->保留[1]
    06-21 14:10:01.380:D/GraphicBuffer(19206):创建句柄(0x617e2de8)(w:544,h:960,f:1)
    06-21 14:10:01.382:D/OpenGLRenderer(19206):启用调试模式0
    06-21 14:10:01.382:I/(19206):[自定义属性]输入状态值
    06-21 14:10:01.382:I/(19206):[自定义属性]重置解析器->保留[1]
    06-21 14:10:01.382:I/(19206):[自定义属性]输入状态值
    06-21 14:10:01.382:I/(19206):[自定义属性]重置解析器->保留[1]
    06-21 14:10:01.382:I/(19206):[自定义属性]输入状态值
    06-21 14:10:01.382:I/(19206):[定制道具]进入状态道具
    06-21 14:10:01.382:I/(19206):[定制道具]道具名称:ro.build.version.release
    06-21 14:10:01.382:I/(19206):[自定义属性]输入状态值
    06