Java 连接到服务器http客户端
我第一次尝试连接到服务器。我从互联网上复制了代码试图执行。但总是在nameValuePairs上显示错误。我不知道这是什么。以及为什么会出现错误。任何人向我解释代码和错误,或解释我通过代码以自己的方式连接到服务器。非常感谢。您必须制作一份NameValuePair列表。。。像这样Java 连接到服务器http客户端,java,android,json,apache,httpclient,Java,Android,Json,Apache,Httpclient,我第一次尝试连接到服务器。我从互联网上复制了代码试图执行。但总是在nameValuePairs上显示错误。我不知道这是什么。以及为什么会出现错误。任何人向我解释代码和错误,或解释我通过代码以自己的方式连接到服务器。非常感谢。您必须制作一份NameValuePair列表。。。像这样 try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://nayyar.5gbfree.com/welcome.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success ");
Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
}
您的帖子必须接收哪些数据?例如,如果url需要“id”和“用户”:
HttpClient-HttpClient=newdefaulthttpclient();
HttpPost HttpPost=新的HttpPost(“http://nayyar.5gbfree.com/welcome.php");
//添加您的数据
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“id”,“12345”);
添加(新的BasicNameValuePair(“用户”,“Jorgesys!”);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
...
...
...
不,我们不会给您代码。当发布你所写代码的问题时,一定要陈述你的目标。如果有错误,请始终发布堆栈跟踪或编译器错误。或者解释它在您的期望方面的表现。这意味着,您应该声明这些nameValuePairs!错误:无法将nameValuePairs解析为变量您是否尝试查找该错误?你真的没有表现出任何努力。
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://nayyar.5gbfree.com/welcome.php");
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
postParameters);
httppost.setEntity(formEntity);
HttpResponse response = httpclient.execute(httppost);
BufferedReader br = new BufferedReader(new InputStreamReader(
httppost.getEntity().getContent()));
br.readLine(); // Save it in a String variable or whatever you want
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://nayyar.5gbfree.com/welcome.php");
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("user", "Jorgesys!"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
...
...
...