Android开发中客户端与内测服务器交互失败

项目:向某邮箱/手机/QQ发送表白信息。需要用到与服务器交互的功能点有:注册、登录、找回密码、加载表白主题、发送表白信息、接收系统消息、加载个人资料等。

以下为登录调用的与Server交互的方法:

/**
	 * 用于用户登录
	 * 
	 * @param userName
	 * @param password
	 * @return
	 * @throws ParseException 
	 * @throws IOException
	 */
	public String doLogin(String name, String pass) throws IOException  {
		client = new DefaultHttpClient();
		httpPost = new HttpPost("http://192.168.2.100:81/index.php/contract/index");//登录功能仅用到这个地址(内测时要改为测试服务器地址) 	         httpPost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
		HttpEntity entity = null;
		String test=null;

		/** 声明登录的参数 */
		List<NameValuePair> list = new ArrayList<NameValuePair>();
		list.add(new BasicNameValuePair("uid", name));
		list.add(new BasicNameValuePair("password", pass));
	try{
		try{
			try{
			httpPost.setEntity(new UrlEncodedFormEntity(list, HTTP.UTF_8));
			/** 执行http发送数据 */
			response = client.execute(httpPost);
			/** 获取cookie数据 */
			Header[] header = response.getHeaders("Set-Cookie");
			entity = response.getEntity();
			test = EntityUtils.toString(entity, "UTF-8");
		         entity.consumeContent();
	} catch(InterruptedIOException e){
		Log.v("returnString->", e.toString());
		test="";
		//e.printStackTrace();
	}
	} catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		test="";
		e.printStackTrace();
	}			
    } catch (ClientProtocolException e) {
		// TODO Auto-generated catch block
    	test="";
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		test="";
		e.printStackTrace();
	}
		return test;
	}
	

以下为其他功能点与Server交互时调用的公用方法:

public String doPost(String xmlString, String userName, String password) throws IOException   {
  client = new DefaultHttpClient();
//  client.getCredentialsProvider().setCredentials(new AuthScope(domain, 80), 
//    new UsernamePasswordCredentials(userName, password));
  /** 创建httpGet */
  httpPost = new HttpPost("/index.php/mobile/microblog");//非登录的功能点用到的2个Server地址之一
  httpPost.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE,
    false);
  host = new HttpHost("192.168.2.100", 81, "http");//非登录的功能点用到的2个Server地址之一。参数1为服务器IP(内测时要改为测试服务器IP),参数2为服务器端口(内测时要改为测试服务器端口),参数3的http会拼到网页地址中。

  byte[] encodedPassword    =  (userName + ":" + password).getBytes();
  httpPost.setHeader("Authorization", "Basic " + Base64.encodeBytes(encodedPassword).toString());

  StringEntity stringEntity;
  String logString = null;
  HttpEntity entity = null;
  stringEntity = new StringEntity(xmlString, HTTP.UTF_8);
  httpPost.setEntity(stringEntity);
  response = client.execute(host, httpPost);
  entity = response.getEntity();
  logString = EntityUtils.toString(response.getEntity(), "UTF-8");
  entity.consumeContent();
  return logString;
}

用户端与测试服务器联调时,怎么也连接不上测试服务器,无法与Server交互。所谓与服务器的交互,一定要用到服务器相关的地址,正确修改相关的地址,也就能正确地连接到测试服务器。问题的解决如下:

1.上述方法里提到的内测应改的地址没有写IP,写的是域名如"test.3jiu.com",而这个域名与IP的映射是需要在hosts文件里配置的。手机中的hosts文件没有配,当然无法识别。可以试着配一下,但我配了以后仍然无法识别,遂直接改用IP地址;

2..改为IP地址后,问题依然存在。原来是传递给Server某方法的参数的排序有误(如用户端找回密码时要给Server端的找回密码方法传递参数);

3.参数正确排序,问题依然存在。原来是host = new HttpHost("192.168.2.100", 81, "http")中的端口号仍写的是正式服务器的端口号80而非测试服务器的端口号81;

4.端口号也修改后,问题解决,与测试服务器交互成功。该问题解决的关键是没有清晰理解host = new HttpHost("192.168.2.100", 81, "http")中各参数的含义,所以以后定位解决问题不能草草了事,要理解代码逻辑。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值