Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.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 我的项目赢了';不能通过HttpPost_Java_Android_Httprequest - Fatal编程技术网

Java 我的项目赢了';不能通过HttpPost

Java 我的项目赢了';不能通过HttpPost,java,android,httprequest,Java,Android,Httprequest,在我的CountryActivity.java中,我有一个HttpRequest来检索维基百科的json信息。 这是我使用的代码AsyncTask: private class DownloadFilesTask extends AsyncTask<URL, Integer, String> { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new Http

在我的
CountryActivity.java
中,我有一个HttpRequest来检索维基百科的json信息。 这是我使用的代码
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 "));
    }
}
私有类下载文件任务扩展异步任务{
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=Portugal");
受保护的字符串doInBackground(URL…URL){
HttpResponse HttpResponse=null;
试一试{
httpResponse=httpClient.execute(httpPost);
}捕获(IOE异常){
e、 printStackTrace();
}
HttpEntity HttpEntity=httpResponse.getEntity();
试一试{
is=httpEntity.getContent();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
系统输出打印项次(行);
}
is.close();
使某人返回字符串();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(字符串结果){
showDialog(Integer.parseInt(“下载”);
}
}
并且,要在我的活动中调用该类,我使用
newdownloadfilesstask()。
问题是,当我调试我的私有类时,调试器在
HttpPost-HttpPost=new-HttpPost(“https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=Portugal");

这是我的日志:

使用
HttpURLConnection
打开与url的连接,并将
setRequestMethod()
设置为
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");
然后获取其输入流,并通过
BufferedReader
读取到构建中

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

检查以了解batter。

为什么要发布数据,如果要获取json,我的意思是使用get方法。正如@Mohit所说,要从源代码获取数据,需要使用
HttpGet
HttpPost
用于发送数据。@RoadEx Ok,我将其更改为
HttpGet-HttpGet=new-HttpGet(“https://en.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro=&explaintext=&titles=Portugal");但我仍然遇到同样的问题…如果这是一个愚蠢的问题,很抱歉,但是,我将这些代码行放在我的onCreate或问题中的类中?顺便说一句,URL会显示“格式不正确的URL表达式”,以防它不起作用……明白了……不要把它放在你的onCreate中,你的应用程序会崩溃,因为这是UI线程中不允许的网络任务,把所有的东西都放在
doInBackground
中,以及我拥有的其他东西,我是否要删除所有这些东西,只留下类,doInBackground和该代码?您可以将url传递到
AsyncTask
,并创建一个方法来获取json,就像在示例链接中完成的那样