Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.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解析器使用线程,不会进入android的run方法_Java_Android_Json_Threadpool - Fatal编程技术网

Java json解析器使用线程,不会进入android的run方法

Java json解析器使用线程,不会进入android的run方法,java,android,json,threadpool,Java,Android,Json,Threadpool,我是编程新手,我正在制作一个json解析器,但是我的线程方法是不可访问的,我该怎么做呢。当我调试时,它进入我的getjson方法,但随后跳过run方法。 我已经在StackOverFlow上搜索过了,但我对线程非常困惑,有什么好方法可以做到这一点吗 public class jsonParser { static InputStream is = null; static JSONObject jObj = null; static String json = "";

我是编程新手,我正在制作一个json解析器,但是我的线程方法是不可访问的,我该怎么做呢。当我调试时,它进入我的getjson方法,但随后跳过run方法。 我已经在StackOverFlow上搜索过了,但我对线程非常困惑,有什么好方法可以做到这一点吗

public class jsonParser {
 static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
    // constructor
    public jsonParser() {

    }

    public JSONObject getJSONFromUrl(final String url) {

         Thread t = new Thread() {

public  void run() {
Looper.prepare(); //For Preparing Message Pool for the child Thread
       // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object

            try {jObj = new JSONObject(json);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
Log.e("JSON Parser", "Error parsing data " + e.toString());     
                 }
            Looper.loop(); 
       return;     
      }
}; t.start();
return jObj;      
         }
                }
它转到
run()
方法。但是线程的原理是它与当前线程并行执行。下面是在时间线上发生的事情:

main thread
_______________________________________________________________________
    |                    |
    start parser thread  return jObj

parser thread
      _________________________________________________________________
       |                                                       |
       execute an HTTP request and parse json                  assign JSON object to jObj
当您从当前线程返回
jObj
时,您刚刚启动了解析器线程。也许它还没有开始执行。也许它还在等待HTTP响应。可以确定的是,它有0%的几率已经完成执行并将结果存储在静态jObj变量中。因此,从当前线程返回的值为null


除了阅读更多关于线程和android提供的支持类之外,很难告诉您应该做什么。

您尝试过gson库吗?线程的要点是并行运行。。。您只能调试一个线程,因此,当您调试主线程时,另一个线程在您看不到的情况下并行运行。在run方法中放置一个断点,您将看到run方法正在运行。考虑一下一些关于线程的教程。嘿!它没有触及“正在运行”字段,我不知道如何前进。非常感谢您的清晰性。您可以提出其他方法来实现这一点吗?首先,不要使用未同步的静态变量在线程之间共享状态。只使用局部变量。我不知道你想要实现什么,但是当前线程不应该从这个方法返回任何东西。相反,解析器线程应该在完成JSON解析后调用一些回调方法(例如,记录它或在屏幕上显示它)。