用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

网络请求方式

2016-12-16 作者: cwm1996举报

[android]代码库

方法一:HttpURLConnection  GET请求
////////////////////////////////////////////////////////////
private void getdata() {
		dialog = new ProgressDialog(getActivity());
		dialog.setMessage("正在加载...");
		dialog.show();
		new  Thread() {

			public void run() {
				// android4.0之后,耗时操作放在子线程里
				URL url;
				try {
					url = new URL("http://v.juhe.cn/weixin/query?key=484057f2721591bc4730f6b370c7ff31&pno="+num);
					 try {
						HttpURLConnection connection=(HttpURLConnection) url.openConnection();
						//连接时间
						connection.setConnectTimeout(5000);
						//读取时间
						connection.setReadTimeout(5000);
						//获取请求码
						int code = connection.getResponseCode();
						//请求码为200即成功
						if(code==200){
							//转换成字节流
							InputStream inputStream = connection.getInputStream();
							//字节数组输出流
							ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
							byte[] b=new byte[1024];
							int len=0;
							//循环读取
							while((len=inputStream.read(b))!=-1){
								//写入字节数组
								byteArrayOutputStream.write(b, 0, len);
							}
							//转换成字符串
							String string = byteArrayOutputStream.toString("utf-8");
							//解析
							Gson gson=new Gson();
							Bean bean = gson.fromJson(string, Bean.class);					
							//发送消息
							handler.obtainMessage(0, bean).sendToTarget();
						}
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}           
			};
		}.start();
	}
方法二:HttpURLConnection  POST请求             耗时操作放在线程中
////////////////////////////////////////////////////////
//请求路径
            URL url=new URL("http://v.juhe.cn/weixin/query?");
//获取HttpURLConnection 
            HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
//请求方式
            httpURLConnection.setRequestMethod("POST");
//连接读取耗时
			httpURLConnection.setConnectTimeout(5000);
			httpURLConnection.setReadTimeout(5000);
//将参数写入
			String str="key=484057f2721591bc4730f6b370c7ff31&pno="+num;
			httpURLConnection.setDoOutput(true);
			OutputStream outputStream = httpURLConnection.getOutputStream();
			outputStream.write(str.getBytes());
//请求码
			int code = httpURLConnection.getResponseCode();
			if(code==200){
				InputStream inputStream = httpURLConnection.getInputStream();
				BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
				String s=null;
				StringBuilder builder=new StringBuilder();
				while((s=reader.readLine())!=null){
					builder.append(s);
				}
				String string=builder.toString().trim();
			}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
方法三:HttpClient GET请求
new Thread() {

			public void run() {
				// android4.0之后,耗时操作放在子线程里
				try {
					HttpClient httpClient = new DefaultHttpClient();
					String path = "http://apis.juhe.cn/train/s2swithprice?start="
							+ start
							+ "&end="
							+ end
							+ "&key=6a12c4348d9174020aa24cac0d410355";
					HttpGet httpGet = new HttpGet(path);
					HttpResponse httpResponse = httpClient.execute(httpGet);
					int code = httpResponse.getStatusLine().getStatusCode();
					if (code == 200) {
						InputStream inputStream = httpResponse.getEntity()
								.getContent();
						Gson gson = new Gson();
						Bean bean = gson.fromJson(new InputStreamReader(
								inputStream), Bean.class);
						list = bean.result.list;
						handler.sendEmptyMessage(0);
					}
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			};

		}.start();
方法四:HttpClient POST请求
    new Thread() {
			private Data data;

			public void run() {
				// android4.0之后,耗时操作放在子线程里
				try {
					HttpClient httpClient = new DefaultHttpClient();
					HttpPost httpPost = new HttpPost(	"http://japi.juhe.cn/qqevaluate/qq");
					// 集合
					List<BasicNameValuePair> list = new ArrayList<BasicNameValuePair>();
					// 添加参数
					list.add(new BasicNameValuePair("key","b4f60e610563b871fd43b615b54f727a"));
					list.add(new BasicNameValuePair("qq", qq));
					// 发送实体对象
					HttpEntity entity = new UrlEncodedFormEntity(list, "utf-8");
					httpPost.setEntity(entity);
					try {
						// 发送请求
						HttpResponse httpResponse = httpClient.execute(httpPost);
						// 获取响应码
						int code = httpResponse.getStatusLine().getStatusCode();
						if (code == 200) {
							// 获取实体信息
							HttpEntity entity2 = httpResponse.getEntity();
							String string = EntityUtils.toString(entity2,"utf-8");
							Gson gson = new Gson();
							Bean bean = gson.fromJson(string, Bean.class);
							data = bean.result.data;
							runOnUiThread(new Runnable() {
								public void run() {
									t.setText("QQ测试结果:" + data.conclusion);
									t2.setText("QQ测试分析:" + data.analysis);
								}
							});
						} else {
							Toast.makeText(MainActivity.this, "无qq参数或者格式不正确", 0)
									.show();
						}
					} catch (ClientProtocolException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			};

		}.start();
方法五:httpUtils    GET请求
String string="http://v.juhe.cn/weixin/query?key=484057f2721591bc4730f6b370c7ff31&pno=1";
		HttpUtils httpUtils=new HttpUtils();
		httpUtils.send(HttpMethod.GET, string, new RequestCallBack<String>() {
			//联网失败
			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub
			}
			//联网成功
			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// TODO Auto-generated method stub
				String result = arg0.result;
				Log.i("TAG", result);
			}
		});
//方法四:httpUtils    POST请求
HttpUtils httpUtils=new HttpUtils();
		RequestParams params=new RequestParams();
		params.addBodyParameter("key", "484057f2721591bc4730f6b370c7ff31");
		params.addBodyParameter("pno", "1");
		httpUtils.send(HttpMethod.POST, "http://v.juhe.cn/weixin/query", params, new RequestCallBack<String>() {
			//联网失败
			@Override
			public void onFailure(HttpException arg0, String arg1) {
				// TODO Auto-generated method stub
			}
			//联网成功
			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				// TODO Auto-generated method stub
				String result = arg0.result;
				Log.i("TAG", result);
			}
		});


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...