1

我正在尝试使用 JELLYBEAN API 17 为 android 应用程序下载 json 数据。

我还在清单文件中添加了互联网使用权限。

这是在活动中获取 json 数据的代码:

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //final Button btnLoad = (Button) findViewById(R.id.btnLoad);
    tvResult = (TextView) findViewById(R.id.txtView);
    //

}

   public void onClick(View v) {
    final LoadData data = new LoadData();
        Log.d("TAG","Button Pressed!");


                data.execute();
                tvResult.setText(data.get());


    }

public static  String getJSON(String url) throws IOException {
    URL u = new URL(url);
    HttpURLConnection c = (HttpURLConnection) u.openConnection();
    try {
        c.setRequestMethod("GET");
        c.setRequestProperty("Content-length", "0");
        c.setUseCaches(false);
        c.setAllowUserInteraction(false);

        c.connect();
        //int status = c.getResponseCode();


            BufferedReader br = new BufferedReader(new InputStreamReader(
                    c.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line ="";
            while ((line = br.readLine()) != null) {
                sb.append(line + "\n");
            }
            br.close();
            return sb.toString();


    }finally{
    c.disconnect();
    }
}
// Inner class for making an asyncTask
public class LoadData extends AsyncTask<Void, Void, String> {

    @Override
    protected String doInBackground(Void... params) {
        final TMDb tmdb = new TMDb();
        String data = "";
        try {
             data = getJSON("http://api.themoviedb.org/3/genre/list?api_key=#");                
         tvResult.setText(data);

          Log.d("TAG-DATA", data);
          Log.d("TAG", "RUN STARTED!");

        } catch (IOException e) {
            Log.d("ERROR", e.getMessage());
            e.printStackTrace();
        }
        return data;
    }

}

这适用于常规的 java 应用程序。但在 android 应用程序中往往会杀死活动。它产生了以下错误:

D/TAG(13771): Button Pressed!
W/dalvikvm(13771): threadid=11: thread exiting with uncaught exception (group=0x40bce930)
 E/AndroidRuntime(13771): FATAL EXCEPTION: Thread-6603
 E/AndroidRuntime(13771): java.lang.NullPointerException: println needs a message
 E/AndroidRuntime(13771):   at android.util.Log.println_native(Native Method)
 E/AndroidRuntime(13771):   at android.util.Log.d(Log.java:138)
 E/AndroidRuntime(13771):   at com.mjavacam.tmdb.MainActivity$1.run(MainActivity.java:88)
 E/AndroidRuntime(13771):   at java.lang.Thread.run(Thread.java:856)

任何人都可以通过向我展示以下内容来提供帮助:

  • 如何正确地从 android 应用程序发出 http 请求?

  • 确定我可能做错了什么?

4

1 回答 1

2

错误在 Log.d("Tag Data",data); 您从 getJson 类返回的数据字符串返回 null。

You can change the reading part(i.e reading the response from http://api.themoviedb.org/3/genre/list?api_key=#) as follows Instead of using BufferedReader you can use this

HttpResponse response;
HttpPost post = new HttpPost("http://api.themoviedb.org/3/genre/list?api_key=#");
HttpEntity httpEntity = response.getEntity();
String result = EntityUtils.toString(httpEntity);

Now you can play around with string result.

于 2013-03-05T03:35:05.360 回答