解决方案 »
- android睡眠醒来后,如何不现实解锁界面,直接进到其他界面
- Android.mk中如何输出信息
- 关于华为的几款手机如何产生调制解调器或者端口的问题
- Android手机的Socket通信严重延时
- 怯问一句:如果不使用ContentProvider机制,SQLite数据库本身就可以在不同程序间共享数据吗?
- apk在真机上运行不了
- Android 摄像头问题
- android自定义半透明标题栏
- Android studio模拟器运行不了项目问题
- webview页面因wifi导致进程被杀死 , 进来看看你的app有么有这种情况吧!
- 如何实现,向下滑动,就会显示一个界面
- 请问 在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;
}