2016-02-15 24 views
0

我正在学习使用JSON解析来自URL的数据。 在下面的代码中,我是URLConnection类对象以从URL获取数据。无法使用JSON获取解析数据

该代码不显示任何错误,但在执行时会产生空白屏幕。 我无法理解为什么会发生。

这是代码。

package com.example.asad.parse; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.util.Log; 
import android.widget.TextView; 

import org.json.JSONArray; 
import org.json.JSONObject; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.URL; 
import java.net.URLConnection; 

public class MainActivity extends AppCompatActivity { 
    TextView tv; 
    int count=0; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Log.d("asd", "calling call"); 

     new GetJson().execute("http://api.androidhive.info/json/movies.json"); 
//  try{ 
// 
//    call(); 
//   }catch(Exception e) { 
// 
//  } 

    } 
    public class GetJson extends AsyncTask<String, Void, Void> 
    { 
     @Override 
     protected Void doInBackground(String... params) { 
      Log.d("asd", params[0]); 
//   Log.d("asd","outside try"); 
      StringBuffer sb=new StringBuffer(); 
      URL url= null; 
      try { 
       Log.d("asd","inside call"); 
       url = new URL(params[0]); 

       URLConnection tc = url.openConnection(); 
       BufferedReader br = new BufferedReader(new InputStreamReader(
         tc.getInputStream())); 

       Log.d("asd","above while."); 
       String line; 
       while ((line = br.readLine()) != null) { 

        Log.d("asd","inside while."); 
        JSONArray ja = new JSONArray(line); 
        for (int i = 0; i < ja.length(); i++) { 
         JSONObject jo = (JSONObject) ja.get(i); 
         Log.d("asd",jo.toString()); 
        } 
       } 
// 
//   tv=(TextView)findViewById(R.id.textView); 
//   tv.setText(sb); 
       Log.d("asd",sb.toString()); 

      } catch (Exception e) { 
       e.printStackTrace(); 
       Log.d("asd", "I am in catch"); 

      } 
      return null; 
     } 
    } 
} 
+0

您是否尝试在浏览器中运行url以查看json是否正确显示? –

+0

什么是Log.d(“asd”,params [0])和Log.d(“asd”,jo.toString())输出? – Kuvalya

+0

params [0]打印我指定的URL,因为代码无法进入while循环,这就是jo.toString()不打印任何内容的原因。 – a874

回答

1

建议JSON使用StringBuilder。 可能有几件事情你的代码错误:由线

  1. 权限在mainfest.xml

    <uses-permission android:name="android.permission.INTERNET"/> 
    
  2. 读书线不是JSON完美的解决方案。

使用静态方法来从URL

public static String getData(String uri) { 
    BufferedReader reader = null; 
    try { 
     URL url = new URL(uri); 
     HttpURLConnection con = (HttpURLConnection) url.openConnection(); 

     StringBuilder sb = new StringBuilder(); 
     reader = new BufferedReader(new InputStreamReader(con.getInputStream())); 
     String line; 

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

     return sb.toString(); 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
     return null; 

    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 

    } finally { 
     if (reader != null) { 
      try { 
       reader.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       return null; 
      } 
     } 
    } 
} 

返回完整JSON文本之后,你可以通过解决右边的数组/对象的JSON文本循环。 内您的AsyncTask:

String jsonText = getData("http://api.androidhive.info/json/movies.json"); 
try { 
     JSONArray jsonArray = new JSONArray(jsonText); 
     ... 
} 

后开始阅读JSON对象

0

更新视图,得到响应后有一个textview和setText。

+0

问题是代码无法在while循环中输入。我只想知道为什么? – a874

0

一切都是按照这一JAVA DOC

测试一下清单文件internet许可看起来不错:

<uses-permission android:name="android.permission.INTERNET"/> 
0

你在你的catch子句中加入了一条日志语句,但你不会看它的写法...... 首先使用StringBuffer和BufferedReader构建整个字符串,然后才将其转换为json数组或对象。

URLConnection tc = url.openConnection(); 
     BufferedReader br = new BufferedReader(new InputStreamReader(tc.getInputStream())); 

     String line; 
     while ((line = br.readLine()) != null) { 
      sb.append(line); 
     } 

     JSONArray ja = new JSONArray(sb.toString()); 
     for (int i = 0; i < ja.length(); i++) { 
      JSONObject jo = (JSONObject) ja.get(i); 
      Log.d("asd", jo.toString()); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    }