My program won't pass through HttpPost

问题: In my CountryActivity.java I have a HttpRequest to retrieve json information of the wikipedia. This is the code I use AsyncTask: private class DownloadFilesTask extend...

问题:

In my CountryActivity.java I have a HttpRequest to retrieve json information of the wikipedia. This is the code I use AsyncTask:

   private class DownloadFilesTask extends AsyncTask<URL, Integer, String> {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=Portugal");
    protected String doInBackground(URL... urls) {
        HttpResponse httpResponse = null;
        try {
            httpResponse = httpClient.execute(httpPost);
        } catch (IOException e) {
            e.printStackTrace();
        }

        HttpEntity httpEntity = httpResponse.getEntity();
        try {
            is = httpEntity.getContent();
        } 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");
                System.out.println(line);
            }
            is.close();
            return sb.toString();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute(String result) {
        showDialog(Integer.parseInt("Downloaded "));
    }
}

And, to call the class in my activity I use new DownloadFilesTask();. The problem is, when I debug my private class, the debugger stops in the line HttpPost httpPost = new HttpPost("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=Portugal"); and it can't even retrieve the json. Do you know what may be happening? My app doesn't crash or nothing...

This is my logcat: https://pastebin.com/EgVrjfVx


回答1:

Open connection to url with HttpURLConnection and set setRequestMethod() to GET

URL obj = new URL("https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=Portugal");
HttpURLConnection http = (HttpURLConnection) obj.openConnection();      
http.setRequestMethod("GET");

then gets its input stream and read via BufferedReader to your build.

BufferedReader reader = new BufferedReader(new InputStreamReader(http.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
    sb.append(line + "n");
}
reader.close();
String json_string = sb.toString();           // your json data

Check here full example to understand batter.

  • 发表于 2018-07-05 12:05
  • 阅读 ( 326 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除