2012-06-14 174 views
1

我是新來的android編程,我正在嘗試開發一個應用程序,調用此Web服務,傳遞值,並獲取一個XML形式的返回,但每次都有一個請求我得到作爲數據輸入的返回無效。我試圖通過HTTP get和Post來完成,但兩者都不是wrking。該鏈接如下,讓Http獲取和發佈請求

http://hiscentral.cuahsi.org/webservices/hiscentral.asmx?op=GetSeriesCatalogForBox2

而且代碼,

HttpGet: 

public class Main extends Activity implements OnClickListener 
{ 
String xml=null; 
String responseBody; 
String text = null; 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    findViewById(R.id.my_button).setOnClickListener(this); 
} 

public void onClick(View arg0) 
{ 
    Button b = (Button)findViewById(R.id.my_button); 
    b.setClickable(false); 
    new LongRunningGetIO().execute(); 
} 

private class LongRunningGetIO extends AsyncTask <Void, Void, String> 
{ 


    @Override 
    protected String doInBackground(Void... params) 
    { 

try 
     { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpContext localContext = new BasicHttpContext(); 
     HttpGet httpGet = new  HttpGet("http://hiscentral.cuahsi.org/webservices/hiscentral.asmx/GetSeriesCatalogForBox2?xmin=-100 &xmax=-90 &ymin=40 &ymax=55 &conceptKeyword=precipitation &beginDate=1/1/2009&endDate=1/1/2010 HTTP/1.1"); 
     HttpResponse response = httpclient.execute(httpGet, localContext); 
      HttpEntity entity = response.getEntity(); 
      text = getASCIIContentFromEntity(entity); 
     } 

     catch (Exception e) 
     { 
      return e.getLocalizedMessage(); 
     } 
     return text; 
    } 



    catch (ClientProtocolException e) 
        { 
         e.printStackTrace(); 
        } 
        catch (IOException e) 
        { 
         e.printStackTrace(); 
        } 

       } 
      catch (UnsupportedEncodingException e) 
      { 
       e.printStackTrace(); 
       } 
      return responseBody; 
    } 
     protected void onPostExecute(String results) 
        { 
         if (results!=null) 
         { 
          EditText et = (EditText)findViewById(R.id.my_edit); 
          et.setText(results); 
         } 
         Button b = (Button)findViewById(R.id.my_button); 
         b.setClickable(true); 
        } 
       } 
      } 

和HttpPost是,

HttpPost 
public class Main extends Activity implements OnClickListener 
{ 
String xml=null; 
String responseBody; 
String text = null; 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
findViewById(R.id.my_button).setOnClickListener(this); 
} 

public void onClick(View arg0) 
{ 
Button b = (Button)findViewById(R.id.my_button); 
b.setClickable(false); 
new LongRunningGetIO().execute(); 
} 

private class LongRunningGetIO extends AsyncTask <Void, Void, String> 
{ 


@Override 
protected String doInBackground(Void... params) 
{ 
HttpClient httpclient = new DefaultHttpClient(); 
     String Url = "http://hiscentral.cuahsi.org/webservices/hiscentral.asmx?op=GetSeriesCatalogForBox2"; 
     if(!Url.endsWith("?")) 
     { 
       Url += "?"; 
     } 
     HttpPost httppost = new HttpPost(Url); 
      // Add data 

      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5); 
      nameValuePairs.add(new BasicNameValuePair("xmin", "-100")); 
      nameValuePairs.add(new BasicNameValuePair("xmax", "-90")); 
      nameValuePairs.add(new BasicNameValuePair("ymin", "40")); 
      nameValuePairs.add(new BasicNameValuePair("ymax", "55")); 
      nameValuePairs.add(new BasicNameValuePair("networkIDs", "")); 
      nameValuePairs.add(new BasicNameValuePair("conceptKeyword", "precipitation")); 
      nameValuePairs.add(new BasicNameValuePair("beginDate", "1/1/2009")); 
      nameValuePairs.add(new BasicNameValuePair("endDate", "1/1/2010")); 
      try 
      {   
         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
        // Log.d("myapp", "works till here. 2"); 
         HttpResponse response = httpclient.execute(httppost); 
         responseBody = EntityUtils.toString(response.getEntity()); 
        // Log.d("myapp", "response " + response.getEntity()); 
        // HttpEntity entity = response.getEntity(); 
         // xml = getASCIIContentFromEntity(entity); 
        } 
        catch (Exception e) 
        { 
         return e.getLocalizedMessage(); 
        } 
      return responseBody; 
    } 

        protected void onPostExecute(String results) 
        { 
         if (results!=null) 
         { 
          EditText et = (EditText)findViewById(R.id.my_edit); 
          et.setText(results); 
         } 
         Button b = (Button)findViewById(R.id.my_button); 
         b.setClickable(true); 
        } 
       } 
      } 

請幫助! 感謝名單中adnvance ..

+0

你正在嘗試下載什麼? JSON XML或BITMAP? – Vervatovskis

+0

你可以發佈你的logcat嗎? – Guardanis

回答

1

您重新失蹤networkIDs參數HTTPGET所以試試這個:

http://hiscentral.cuahsi.org/webservices/hiscentral.asmx/GetSeriesCatalogForBox2?xmin=-100&xmax=-90&ymin=40&ymax=55&conceptKeyword=precipitation& 
networkIDs=YOUR_NETWORK_IDS&beginDate=1/1/2009&endDate=1/1/2010 

代替

http://hiscentral.cuahsi.org/webservices/hiscentral.asmx/GetSeriesCatalogForBox2?xmin=-100&xmax=-90&ymin=40&ymax=55& 
conceptKeyword=precipitation&beginDate=1/1/2009&endDate=1/1/2010%20HTTP/1.1 

,並在HttpPost改變你的網址爲:

String Url = "http://hiscentral.cuahsi.org/webservices/hiscentral.asmx/GetSeriesCatalogForBox2"; 
+0

看到我的編輯答案 –

+0

thanx很多..現在HttpPost工作正常... 有關HttpGet請求,網絡ID被提及爲可選...所以有任何想法,爲什麼這是行不通的? –

+0

是的參數值是可選的非參數,所以如果你不想傳遞networkids,那麼使用這個'http://hiscentral.cuahsi.org/webservices/hiscentral.asmx/GetSeriesCatalogForBox2?xmin=-100&xmax=-90&ymin=40&ymax = 55&conceptKeyword = precipitation& networkIDs =&beginDate = 1/1/2009&endDate = 1/1/2010' –