0

我在 android 中编写了一些代码,我正在尝试从网站获取 http 响应。我做到了,但是当我将应用程序上传到我的 android 设备时,来电时我无法读取响应。实际上,我想在来电时在屏幕上显示 http 响应。请帮助我谢谢......

我的 HTTPPOST 和阅读代码

    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

    StrictMode.setThreadPolicy(policy); 


    InputStream is = null;
     String result = "";
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

     try 
     {  HttpParams params = new BasicHttpParams();
     params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
           HttpClient httpclient = new DefaultHttpClient(params);
           HttpPost httppost = new HttpPost("http://www.whois118.com/broadcast.php");
           nameValuePairs.add(new BasicNameValuePair("telefon",tlf));
           nameValuePairs.add(new BasicNameValuePair("hash", hash));
           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
           HttpResponse response = httpclient.execute(httppost);
           HttpEntity entity = response.getEntity();

           is = entity.getContent();
           int length;
      }

      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);
           StringBuilder sb = new StringBuilder();
           String line = null;

       while ((line = reader.readLine()) != null) 
       {
           sb.append(line + "\n");
       }

        is.close();
        result = sb.toString();



       Log.v("log","Result :"+result);
  } 
  catch (Exception e)
  {
   Log.v("log", "Error converting result " + e.toString());
  }


    return result;
}
4

1 回答 1

0

确保您在 manifest.xml 中有足够的权限:<uses-permission android:name="android.permission.INTERNET" />

于 2013-01-26T17:23:26.330 回答