利用java ,apache和android的方法来获取网络数据

来源:互联网 发布:dk系列图书 知乎 编辑:程序博客网 时间:2024/06/09 15:48
     <TextView         android:id="@+id/txtView"         android:layout_width="wrap_content"         android:layout_height="wrap_content"          />     <Button          android:layout_width="match_parent"         android:layout_height="wrap_content"         android:onClick="buttonback"         android:text="点击获取返回数据"/>  //使用HTTP的post方法public class HttpUrlConnectionAct extends Activity {     private TextView mTextView;     private Button button;     //某一服服務端的地址(本地地址/服务端端口/服务端文件夹名(web)/web文件夹里自动生成的web.xml里的自定义名)     private String httpUrl = "http://192.168.1.148:8080/ServletProject/firstservlet";      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         mTextView = (TextView) findViewById(R.id.txtView);      } //android按鈕点击监听     public void buttonback(View v) {         new AsyncTask<String, Void, String>() {             @Override             protected String doInBackground(String... params) {                  String content = getApacheData(httpUrl);                 Log.e("content", content + " ");                 return content;             }              @Override             protected void onPostExecute(String result) {                  mTextView.setText(result);             }          }.execute(httpUrl);      }      /* 利用apache获取网络数据 */     private String getApacheData(String url) {          Log.e("aa", "sssss");         StringBuffer sb = new StringBuffer();         HttpClient httpClient = new DefaultHttpClient();                  //用post传数据         HttpPost httpPost = new HttpPost(url);         BasicNameValuePair username = new BasicNameValuePair("username",                 "general");         BasicNameValuePair password = new BasicNameValuePair("password",                 "12315");         BasicNameValuePair sex = new BasicNameValuePair("sex", "男");         ArrayList<BasicNameValuePair> parPairs = new ArrayList<BasicNameValuePair>();         parPairs.add(username);         parPairs.add(password);         parPairs.add(sex);          Log.e("bb", "sssss");         UrlEncodedFormEntity urlef = null;         try {             urlef = new UrlEncodedFormEntity(parPairs, "utf-8");         } catch (UnsupportedEncodingException e1) {                          e1.printStackTrace();         }         httpPost.setEntity(urlef);         HttpResponse httpResponse = null;         try {             httpResponse = httpClient.execute(httpPost);         } catch (ClientProtocolException e1) {              e1.printStackTrace();         } catch (IOException e1) {              e1.printStackTrace();         }          Log.e("cc", "sssss");          try {              HttpEntity httpEntity = httpResponse.getEntity();             //用输入流接收             InputStream is = httpEntity.getContent();              int statusCode = httpResponse.getStatusLine().getStatusCode();             Log.v("statusCode", statusCode + "");             if (statusCode == 200) {                 // 创建包装流                 BufferedReader br = new BufferedReader(                         new InputStreamReader(is));                 // 定义String类型用于存储单行数据                 String line = null;                 // 创建StringBuffer对象用于存储所有数据                 sb = new StringBuffer();                 while ((line = br.readLine()) != null) {                     sb.append(line);                 }             }         } catch (IOException e) {             e.printStackTrace();         }          return sb.toString();      } }  //get方法/*利用apache获取网络数据*/    private String getApacheData(String url){        InputStream is=null;        BufferedReader br=null;        StringBuffer sb = new StringBuffer();        HttpClient httpClient=new DefaultHttpClient();        HttpGet httpGet=new HttpGet(url);        try {            HttpResponse httpResponse=httpClient.execute(httpGet);            HttpEntity httpEntity=httpResponse.getEntity();            is=httpEntity.getContent();            int statusCode=httpResponse.getStatusLine().getStatusCode();            if (statusCode==200){                //创建包装流                br=new BufferedReader(new InputStreamReader(is));                //定义String类型用于存储单行数据                String line=null;                //创建StringBuffer对象用于存储所有数据                sb=new StringBuffer();                while ((line=br.readLine())!=null){                    sb.append(line);                }            }        } catch (IOException e) {            e.printStackTrace();        }        return sb.toString();    }

//JAVA获取网络数据 /*用JAVA从网络获取数据*/    private String getIntentData(String url){        InputStream is=null;        BufferedReader br=null;        StringBuffer sb = null;        HttpURLConnection connection=null;        try {            //封装访问服务器地址            URL url1=new URL(url);            //打开对服务器的链接            connection=(HttpURLConnection)url1.openConnection();            connection.setRequestMethod("GET");            //链接服务器            connection.connect();            //得到输入流            is=connection.getInputStream();            //创建包装流            br=new BufferedReader(new InputStreamReader(is));            //定义String类型用于存储单行数据            String line=null;            //创建StringBuffer对象用于存储所有数据            sb=new StringBuffer();            while ((line=br.readLine())!=null){                sb.append(line);            }        } catch (MalformedURLException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally {            connection.disconnect();            if (br!=null){                try {                    br.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if (is!=null){                try {                    is.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }        return sb.toString();    }//Android获取网络数据/*利用安卓方法获取数据*/    private String getAndroidData(String url){        InputStream is=null;        BufferedReader br=null;        StringBuffer sb=new StringBuffer();        AndroidHttpClient androidHttpClient=AndroidHttpClient.newInstance("");        HttpGet httpGet=new HttpGet(url);        try {            HttpResponse httpResponse=androidHttpClient.execute(httpGet);            HttpEntity httpEntity=httpResponse.getEntity();            is=httpEntity.getContent();            int statusCode=httpResponse.getStatusLine().getStatusCode();            if (statusCode==200){                //创建包装流                br=new BufferedReader(new InputStreamReader(is));                //定义String类型用于存储单行数据                String line=null;                //创建StringBuffer对象用于存储所有数据                while ((line=br.readLine())!=null){                    sb.append(line);                }            }        } catch (IOException e) {            e.printStackTrace();        }        return sb.toString();    }

0 0
原创粉丝点击