在android开发中,如果我们想从服务器获取数据,需要发送一个Get请求;如何在Android开发中发送这个Get请求呢,请参照下面的例子:
-
- String uriAPI = "http://fanyi.youdao.com/openapi.do?keyfrom=huanxing&key=209690980&type=data&doctype=json&version=1.1&q=你好";
-
- HttpGet httpRequest = new HttpGet(uriAPI);
- try
- {
-
- HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
-
- if(httpResponse.getStatusLine().getStatusCode() == 200)
- {
-
- String strResult = EntityUtils.toString(httpResponse.getEntity());
-
- strResult = eregi_replace("(\r\n|\r|\n|\n\r)","",strResult);
- mTextView1.setText(strResult);
- }
- else
- {
- mTextView1.setText("Error Response: "+httpResponse.getStatusLine().toString());
- }
- }
- catch (ClientProtocolException e)
- {
- mTextView1.setText(e.getMessage().toString());
- e.printStackTrace();
- }
- catch (IOException e)
- {
- mTextView1.setText(e.getMessage().toString());
- e.printStackTrace();
- }
- catch (Exception e)
- {
- mTextView1.setText(e.getMessage().toString());
- e.printStackTrace();
- }
这是在Android开发中常用的发送Get请求的方式。