2011-04-21 93 views
0

我使用的代碼Webservice的問題安卓

HttpClient client = new DefaultHttpClient(); 
    HttpGet method = new HttpGet(Url); 
    HttpResponse response = client.execute(method); 

執行的最後一行,應用始終捕獲異常後調用Web服務的JSON。

任何人都可以幫助我嗎?

回答

1
try this 

HttpPost httppost; 
     HttpClient httpclient; 
     List<NameValuePair> nameValuePairs; 

     String userInfo[] = getUserInfo(); 
     String uname = userInfo[0]; 
     String pass = userInfo[1]; 

     if(uname != null && pass != null) { 
      String bytesSent; 
      httppost = new HttpPost(getURL()); 
      httpclient = new DefaultHttpClient(); 
      nameValuePairs = new ArrayList<NameValuePair>(2); 
      String reminderstatus = "P"; 

      nameValuePairs.add(new BasicNameValuePair("doAction", "something")); 
      nameValuePairs.add(new BasicNameValuePair("username",uname)); 
      nameValuePairs.add(new BasicNameValuePair("password", pass)); 


      try {   
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = httpclient.execute(httppost); 
       InputStream is = response.getEntity().getContent(); 
       BufferedInputStream bis = new BufferedInputStream(is); 
       ByteArrayBuffer baf = new ByteArrayBuffer(20); 
       int current = 0; 

       while((current = bis.read()) != -1) { 
        baf.append((byte)current); 
       } 

       bytesSent = new String(baf.toByteArray()); 
      } 
     } 
0

嗨,你能讓我知道你得到了什麼異常。還要檢查是否添加了下面的權限。

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