无法使用http post request Android从URL中提取数据

时间:2013-12-01 21:51:00

标签: java android http-post

我在从服务器获取数据时遇到了严重问题。

我的网址是:

server_url = http://serverurl/_all

如果我从浏览器拨打电话,我可以看到正在打印的数据。所以我想这与我如何提出请求有关。我之前使用过那部分代码,例如在php文件中发出帖子请求时。但现在我的服务器设置不同,我知道服务器工作正常,因为我可以在浏览器和IOS应用程序中获取我的数据。

我试过这个:

try {
    HttpClient httpclient = new DefaultHttpClient();
    @SuppressWarnings("deprecation")
    //HttpPost httppost = new HttpPost(URLEncoder.encode(server_url));
            HttpPost httppost = new HttpPost(server_url);
    //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();
} catch (Exception e) {
    Log.e("log_tag", "Error in http connection " + e.toString());
}
// convert response to string
try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
    sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    is.close();
    result = sb.toString();
    System.out.println("Result:" + result);
} catch (Exception e) {
    Log.e("log_tag", "Error converting result " + e.toString());
}

我得到了这个:

Result : Error 404 not found

然后,搜索SO我尝试了以下内容:

  • 删除http://
  • 将HttpPost行更改为此

    HttpPost httppost = new HttpPost(URLEncoder.encode(server_url));

在这种情况下,我收到此错误:

12-01 21:41:11.772: E/log_tag(28340): Error in http connection java.lang.IllegalStateException: Target host must not be null, or set in parameters. scheme=null, host=null, path=http://ec2-54-194-95-194.eu-west-1.compute.amazonaws.com/backend2/index.php/skicenter/_all
12-01 21:41:11.772: E/log_tag(28340): Error converting result java.lang.NullPointerException: lock == null
12-01 21:41:11.772: E/log_tag(28340): Error parsing data org.json.JSONException: End of input at character 0 of 

_ 字符应该出现问题,以及我缺少的其他内容。

你可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

您确定,您正在为请求使用正确的HTTP谓词吗?也许这是GET请求,您正在尝试使用POST