带有post参数的HTTP请求,用于抛出RuntimeException的在线URL

时间:2013-04-26 16:50:52

标签: android post httprequest unknown-host

我正在制作一个程序,它使用PHP页面连接到数据库,然后通过JSON解析信息。问题是,我在负责网络通信的AsyncTask上遇到了RuntimeException。我正在使用物理安卓设备进行测试。

try {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("URL");
    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("password", "******"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Error appears on this line
        HttpResponse response = httpclient.execute(httppost);

        //JSON Parsing here

    }

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

}
catch (Throwable t)
{
    CharSequence message = "Request failed: " + t.toString();
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}

1 个答案:

答案 0 :(得分:0)

  

**只需检查您在Android Manifest文件中添加以下权限行的位置。 **

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

**它必须是清单标记的子元素。您可能已将其添加到应用程序标记下。 **

参考:click