gpt4 book ai didi

android - 制作 Http Get 和 Post 请求

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:12:07 27 4
gpt4 key购买 nike

我是 android 编程的新手,我正在尝试开发一个调用此 Web 服务的应用程序,传递值并以 xml 的形式获取返回值,但每次发出请求时,我都会将返回值作为数据条目无效。我尝试通过 Http get 和 Post 进行操作,但两者都不起作用。 Url如下,

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);
}
}
}

请帮忙!!提前谢谢..

最佳答案

您在 HttpGet 中缺少 networkIDs 参数,所以试试这个:

    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";

关于android - 制作 Http Get 和 Post 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11036706/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com