解决方案 »
- 救命了,插入数据就报错SQLiteReadOnlyDatabaseException
- 蓝牙socket通信 数据怎么接收不全呢?
- bitmap无限制放大
- HttpURLConnection下载jsp网页出错
- 怎样截获其他程序的触摸事件,或模拟轨迹球
- 请问有高手配置过Sequoyah吗?“localhost:5039: Operation not permitted”如何解决?
- onActivityResult 关于百度地图SDK 不显示 请搞过呢
- eclipse 无法设置doc中文提示
- 求助这个图灵机器人代码??关于java基础的
- android 打开闪光灯为什么一定要用surfaceview
- 如何实现,向下滑动,就会显示一个界面
- 请问 在Android studio 中下面这个问题怎么解决 谢谢
httpclient的实现流程?
socket的实现流程?我就是不太理解怎么实现服务器和Android的数据交互。我也不知道服务器应该用什么,能给一个推荐吗?就是简单的存储用户的账号信息。
手机一点点你得按你的要求改改,主要使用httpclient httprespone
public static String uploadSubmit(String url, Map<String, String> param,
File file) throws Exception {
HttpPost post = new HttpPost(url); MultipartEntity entity = new MultipartEntity();
if (param != null && !param.isEmpty()) {
for (Map.Entry<String, String> entry : param.entrySet()) {
if (entry.getValue() != null
&& entry.getValue().trim().length() > 0) {
entity.addPart(entry.getKey(),
new StringBody(entry.getValue()));
}
}
}
// 添加文件参数
if (file != null && file.exists()) {
entity.addPart("file", new FileBody(file));
}
post.setEntity(entity);
HttpClient httpClient=new DefaultHttpClient();
HttpResponse response = httpClient.execute(post);
//String serverResponse = EntityUtils.toString(response.getEntity()); 这样可以直接获得,但是下面的代码可以获得复杂的数据
int stateCode = response.getStatusLine().getStatusCode();
StringBuffer sb = new StringBuffer();
if (stateCode == HttpStatus.SC_OK) {
HttpEntity result = response.getEntity();
if (result != null) {
InputStream is = result.getContent();
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String tempLine;
while ((tempLine = br.readLine()) != null) {
sb.append(tempLine);
}
}
}
post.abort();
return sb.toString();
}
public String post(String url,Map<String,String> parameter) throws Exception{
Log.i(Constants.LOG_TAG, url);
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
for(String key : parameter.keySet()){
params.add(new BasicNameValuePair(key, parameter.get(key)));
Log.i(Constants.LOG_TAG, key + "-->" + parameter.get(key));
}
String result = "";
HttpPost postMethod = new HttpPost(url);
postMethod.setHeader("Cookie", cookieString);
postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(postMethod); //Send POST Request
Log.i(Constants.LOG_TAG, "http : "+String.valueOf(response.getStatusLine().getStatusCode()));
if(200==response.getStatusLine().getStatusCode()){
result = EntityUtils.toString(response.getEntity(), "utf-8");
}
return result;
}