androidhttp网络访问

  • d0_365497
    了解作者
  • 25.6KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-13 04:59
    上传日期
一款android通过http访问的程序,附带源代码
httpget和httppost.rar
内容介绍
package net.blogjava.mobile; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class Main extends Activity implements OnClickListener { @Override public void onClick(View view) { String url = "http://192.168.17.156:8080/querybooks/QueryServlet"; TextView tvQueryResult = (TextView) findViewById(R.id.tvQueryResult); EditText etBookName = (EditText) findViewById(R.id.etBookName); HttpResponse httpResponse = null; try { switch (view.getId()) { case R.id.btnGetQuery: url += "?bookname=" + etBookName.getText().toString(); HttpGet httpGet = new HttpGet(url); httpResponse = new DefaultHttpClient().execute(httpGet); if (httpResponse.getStatusLine().getStatusCode() == 200) { String result = EntityUtils.toString(httpResponse .getEntity()); tvQueryResult.setText(result.replaceAll("\r", "")); } break; case R.id.btnPostQuery: HttpPost httpPost = new HttpPost(url); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("bookname", etBookName .getText().toString())); httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); httpResponse = new DefaultHttpClient().execute(httpPost); if (httpResponse.getStatusLine().getStatusCode() == 200) { String result = EntityUtils.toString(httpResponse .getEntity()); tvQueryResult.setText(result.replaceAll("\r", "")); } break; } } catch (Exception e) { tvQueryResult.setText(e.getMessage()); } } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btnGetQuery = (Button) findViewById(R.id.btnGetQuery); Button btnPostQuery = (Button) findViewById(R.id.btnPostQuery); btnGetQuery.setOnClickListener(this); btnPostQuery.setOnClickListener(this); } }
评论
    相关推荐
    • android http源代码
      android http源代码 亲测可用
    • android Http请求
      android Http四种请求方式 包括get post httpClient请求
    • Android 发送http请求
      Android发送http请求,适合初学者,源码可运行
    • Android-Http
      Android http 操作,加异步封装,Android http 操作,加异步封装,Android http 操作,加异步封装,Android http 操作,加异步封装,Android http 操作,加异步封装,Android http 操作,加异步封装
    • android async http 源码
      android async http 源码 很方便加入到你自己的工程 对于理解http很有帮助
    • android http 异步请求
      android http 异步请求 含有源代码,方便查阅!使用简单、可靠!
    • android http get
      android http get
    • android-http
      android-http
    • android http server 服务
      一台android设备分享热点,另一台手机连接热点,访问URL发送用户信息,交由http服务端接收
    • AndroidHttp案例
      AndroidHttp案例,适合初学者的AndroidHttp案例,适合初学者,里面有注释,代码清晰明了,导入项目即可使用,亲测可用。