目标主机不能为空或在parameters.scheme=null、host=null中设置

发布于 2024-12-11 11:25:07 字数 2991 浏览 2 评论 0 原文

我收到以下异常:

    Target host must not be null or set in parameters.scheme=null,
host=null,path=/webservices/tempconvert.asmx/FahrenheitToCelsius

我的源代码:

public class ParActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView t=new TextView(this);
        String encodedUrl = null;
      //  setContentView(R.layout.main);

                HttpClient client = new DefaultHttpClient();  
               //String query = "?Fahrenheit=26";
            //  String host = "www.w3schools.com/webservices/tempconvert.asmx/";
            //   encodedUrl = host + URLEncoder.encode(query,"utf-8");
            //  int p=(Integer) null;
              // URI uri = URIUtils.createURI("vv", "www.w3schools.com", 50, "/webservices/tempconvert.asmx", "?Fahrenheit=26", null);

                try{ 
               String postURL = "/webservices/tempconvert.asmx/FahrenheitToCelsius";
                HttpPost post = new HttpPost(postURL);
              // post.addHeader("scheme","vv");
               // post.setHeader("host", "www.w3schools.com");
                String PostData="36";
                StringEntity httpPostEntity = new StringEntity(PostData, HTTP.UTF_8);
                post.setEntity(httpPostEntity);
                post.setHeader("host", "www.w3schools.com");
                post.setHeader("Content-Length", new Integer(PostData.length()).toString());
                post.setHeader("Content-Type", "application/x-www-form-urlencoded");


                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
               // nameValuePairs.add(new BasicNameValuePair("host", "www.w3schools.com"));
                nameValuePairs.add(new BasicNameValuePair("Fahrenheit", "26"));
               post.setEntity(new UrlEncodedFormEntity(nameValuePairs));


               HttpResponse responsePOST = null;
                // Execute HTTP Post Request
               // HttpResponse response = httpclient.execute(post);
                client.getConnectionManager();



                   responsePOST = client.execute(post); 
                HttpEntity resEntity = responsePOST.getEntity();  
                String response=EntityUtils.toString(resEntity);
                response=response.trim();
              //  Log.i("RESPONSE=",response);
                t.setText("response"+response);
                setContentView(t);
        } catch (Exception e) {
            // TODO Auto-generated catch block
           //e.printStackTrace();
            t.setText("ex"+e.getMessage());
        setContentView(t);
        }
    }

}

我想调用 web 服务: http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit

使用 HttpClient。我应该如何将主机和方案作为输入?

请帮忙...

i am getinng the following exception:

    Target host must not be null or set in parameters.scheme=null,
host=null,path=/webservices/tempconvert.asmx/FahrenheitToCelsius

My source code:

public class ParActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView t=new TextView(this);
        String encodedUrl = null;
      //  setContentView(R.layout.main);

                HttpClient client = new DefaultHttpClient();  
               //String query = "?Fahrenheit=26";
            //  String host = "www.w3schools.com/webservices/tempconvert.asmx/";
            //   encodedUrl = host + URLEncoder.encode(query,"utf-8");
            //  int p=(Integer) null;
              // URI uri = URIUtils.createURI("vv", "www.w3schools.com", 50, "/webservices/tempconvert.asmx", "?Fahrenheit=26", null);

                try{ 
               String postURL = "/webservices/tempconvert.asmx/FahrenheitToCelsius";
                HttpPost post = new HttpPost(postURL);
              // post.addHeader("scheme","vv");
               // post.setHeader("host", "www.w3schools.com");
                String PostData="36";
                StringEntity httpPostEntity = new StringEntity(PostData, HTTP.UTF_8);
                post.setEntity(httpPostEntity);
                post.setHeader("host", "www.w3schools.com");
                post.setHeader("Content-Length", new Integer(PostData.length()).toString());
                post.setHeader("Content-Type", "application/x-www-form-urlencoded");


                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
               // nameValuePairs.add(new BasicNameValuePair("host", "www.w3schools.com"));
                nameValuePairs.add(new BasicNameValuePair("Fahrenheit", "26"));
               post.setEntity(new UrlEncodedFormEntity(nameValuePairs));


               HttpResponse responsePOST = null;
                // Execute HTTP Post Request
               // HttpResponse response = httpclient.execute(post);
                client.getConnectionManager();



                   responsePOST = client.execute(post); 
                HttpEntity resEntity = responsePOST.getEntity();  
                String response=EntityUtils.toString(resEntity);
                response=response.trim();
              //  Log.i("RESPONSE=",response);
                t.setText("response"+response);
                setContentView(t);
        } catch (Exception e) {
            // TODO Auto-generated catch block
           //e.printStackTrace();
            t.setText("ex"+e.getMessage());
        setContentView(t);
        }
    }

}

i want to invoke the webservice at:
http://www.w3schools.com/webservices/tempconvert.asmx?op=CelsiusToFahrenheit

using HttpClient.How should i give host and scheme as input?

please help...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放我走吧 2024-12-18 11:25:07

这是您的主机错误
您在 post.setHeader("host", "www.w3schools.com"); 行中传递 "www.w3schools.com" 而必须传递 “http://www.w3schools.com”

您可以从此处

This is your Host error
You are passing "www.w3schools.com" in post.setHeader("host", "www.w3schools.com"); line instead you have to pass "http://www.w3schools.com"

You can refer the same issue from here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文