2014-03-13 492 views
0
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(); 
      } 

我试图第一次连接到服务器。我从互联网复制代码尝试执行。但总是在nameValuePairs上显示错误。我不知道这是什么。以及为什么错误是。有人向我解释代码和错误,或者解释我通过代码以他/她自己的方式连接到服务器。应该非常感激。连接到服务器http客户端

+3

不,我们不会给你code_。当你用你写的代码发布问题时,**总是**陈述你的目标。如果有错误,**总是**发布堆栈跟踪或编译器错误。或者解释一下你的期望如何表现。 –

+0

这意味着,你应该声明那些nameValuePairs! –

+0

错误:nameValuePairs无法解析为变量 –

回答

0

你必须做出的NameValuePair列表...这样

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 
0

哪些数据必须得到您的POST数据?例如,如果url需要“id”和“user”:

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)); 
    ... 
    ... 
    ... 
相关问题