解决方案 »
- Gallery滑动卡,请高手指点
- Android程序背景图片的问题
- 重写 onCreateOptionsMenu(Menu menu) 方法,却不执行。
- 重启可以,关了电源以后再开机就不行
- Window already focused是由于什么引起的?
- 求教 Android手机上怎么实现短信拦截?
- 分享一个原创Android 单机版斗地主源码
- android手机QQ登陆页面之前闪现的那个是怎么实现的啊
- android system.exit(0)退出程序的问题
- android的http中的cookie的问题,打印的结果好奇怪哦?求解惑?问倒了很多大神,大家来帮我看一下。
- 求教arcgis for android高手,导入arcgtisSDK问题。
- 不能创建对话框
package com.example.http_demo;import android.util.Log;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import java.io.IOException;/**
* Created by gaofeng on 14-1-14.
*/
public class HttpCaller {
public String httpGet(String url) {
Log.d("httpGet", "url:" + url);
HttpClient httpClient = new DefaultHttpClient();
HttpGet _httpGet = new HttpGet(url);
try {
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params,20000);
_httpGet.setParams(params); HttpResponse httpResponse = httpClient.execute(_httpGet);
StatusLine _status = httpResponse.getStatusLine(); if (_status.getStatusCode() == 200) {
Log.d("httpGet", "req is 200.");
return EntityUtils.toString(httpResponse.getEntity());
}
} catch (IOException e) {
e.printStackTrace();
return e.getMessage();
}
return null;
}
}
package com.example.http_demo;import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;/**
* gaofeng
*/
public class MyActivity extends Activity { Button btn;
EditText editText;
TextView textView; /**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); btn = (Button) findViewById(R.id.btn1);
editText = (EditText) findViewById(R.id.edit1); btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
run();
}
});
textView = (TextView) findViewById(R.id.content); }
void run() {
final String url = editText.getText().toString();
new Thread() {
public void run() {
String content = new HttpCaller().httpGet(url);
showContent(content);
}
}.start();
}
void showContent(final String content) {
this.runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText(content);
}
});
}
}
如下:
public void captureHtml(String ip) throws Exception {
String strURL = "http://ip.chinaz.com/?IP=" + ip;
URL url = new URL(strURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
InputStreamReader input = new InputStreamReader(httpConn
.getInputStream(), "utf-8");
BufferedReader bufReader = new BufferedReader(input);
String line = "";
StringBuilder contentBuf = new StringBuilder();
while ((line = bufReader.readLine()) != null) {
contentBuf.append(line);
}
String buf = contentBuf.toString();
int beginIx = buf.indexOf("查询结果[");
int endIx = buf.indexOf("上面四项依次显示的是");
String result = buf.substring(beginIx, endIx);
System.out.println("captureHtml()的结果:\n" + result);
}
还有诸如com.ning打头的一个网络库,当然它需要选择网络后台,netty就是常见选择
https://github.com/AsyncHttpClient/async-http-client
至于绑定控件之类的还是自己处理吧
这些都是开源的,用不用看具体需要,没必要自己写,它们的用法都写的很明白
* 根据url获得页面源码, 调用web接口
* @param url 访问地址, 必备参数
* @param method 请求方式post还是get, 默认get
* @param params 参数列表 post必备, 比如:"name=张三&age=18"
* @param sessionInfo 可以保持session, 默认不保持
* @param encoding 编码格式, 默认UTF_8
* @param isLine 得到的源码是否换行, 默认false
* @return
*/
public String sound(String url, String method, String params, String sessionInfo, String encoding, boolean isLine) throws Exception {
Log.e("AbstractActivity", "调用web接口:" + url);
encoding = (isBlank(encoding) ? HTTP.UTF_8 : encoding);
method = (isBlank(method) ? Domain.REQUEST_GET : method.toUpperCase());
String mathStr = "mathRandom=" + Math.random();
if (method.equals(Domain.REQUEST_GET)) {url += (url.indexOf("?") != -1 ? "&" : "?") + mathStr;}
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
if (!isBlank(sessionInfo)) {conn.setRequestProperty("Cookie", sessionInfo);}
conn.setRequestMethod(method);
if (requestTime > 0) {
conn.setConnectTimeout(requestTime);
conn.setReadTimeout(requestTime);
}
if (method.equals(Domain.REQUEST_POST)) {
conn.setDoOutput(true);
OutputStream output = conn.getOutputStream();
output.write((isBlank(params) ? mathStr : params + "&" + mathStr).getBytes(encoding));
output.flush();output.close();
}
String response = FileUtil.get().readFile(conn.getInputStream(), encoding, 1, isLine);
conn.disconnect();
requestTime = Domain.LONG_REQUEST_TIME;
return response;
}