开发者社区> 问答> 正文

如何在android软件登陆,获取用户名和手机ID,通过POST传递服务器,并获得返回信息?

用户登陆,只输入用户名,并且代码在软件启动时已获得手机硬件ID,通过获取用户名和ID,POST传入服务器,

服务器地址:http://117.28.239.31:8007/Controllers/LoginByDIDHandler.ashx
ID格式:123d257b-2998-33a7-9439-2aebc950efe4
求如果通过POST传userName和Did,并获取返回信息。
最好不要第三方类库这样的建议,求完整代码,谢谢!

展开
收起
小旋风柴进 2016-03-05 15:16:43 3860 0
1 条回答
写回答
取消 提交回答
  • private void login ( final String username , final String password ) { 
    if (! netMgr . isNetworkAvailable ()) { 
    Toast . makeText ( this , "请检查网络连接!" , Toast . LENGTH_LONG ). show (); 
    return ; 
    } 
    loginProgress = new ProgressDialog ( this ); 
    loginProgress . setCancelable ( false ); 
    loginProgress . setCanceledOnTouchOutside ( false ); 
    loginProgress = loginProgress . show ( this , null , "登陆中..." ); 
    new Thread ( new Runnable () { 
    @Override 
    public void run () { 
    Log . d ( "yanghongbing" , "start network!" ); 
    HttpClient client = new DefaultHttpClient (); 
    HttpPost httpPost = new HttpPost ( Config . LOGIN_URL ); 
    List < NameValuePair > params = new ArrayList < NameValuePair >(); 
    params . add ( new BasicNameValuePair ( "username" , username )); 
    params . add ( new BasicNameValuePair ( "password" , password )); 
    
    HttpResponse httpResponse = null ; 
    try { 
    httpPost . setEntity ( new UrlEncodedFormEntity ( params , HTTP . UTF_8 )); 
    httpResponse = client . execute ( httpPost ); 
    if ( httpResponse . getStatusLine (). getStatusCode () == 200 ) { 
    Log . d ( "yanghongbing" , "network OK!" ); 
    HttpEntity entity = httpResponse . getEntity (); 
    String entityString = EntityUtils . toString ( entity ); 
    String jsonString = entityString . substring ( entityString . indexOf ( "{" )); 
    Log . d ( "yanghongbing" , "entity = " + jsonString ); 
    JSONObject json = new JSONObject ( jsonString ); 
    sendMessage ( MSG_LOGIN_RESULT , json ); 
    Log . d ( "yanghongbing" , "json = " + json ); 
    } 
    } catch ( UnsupportedEncodingException e ) { 
    Log . d ( "yanghongbing" , "UnsupportedEncodingException" ); 
    e . printStackTrace (); 
    } catch ( ClientProtocolException e ) { 
    Log . d ( "yanghongbing" , "ClientProtocolException" ); 
    e . printStackTrace (); 
    } catch ( IOException e ) { 
    Log . d ( "yanghongbing" , "IOException" ); 
    sendMessage ( MSG_SERVER_UNAVAILABLE , null ); 
    e . printStackTrace (); 
    } catch ( JSONException e ) { 
    Log . d ( "yanghongbing" , "IOException" ); 
    // TODO Auto-generated catch block 
    e . printStackTrace (); 
    } 
    } 
    }). start (); 
    
    } 
    2019-07-17 18:53:35
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
如何运维千台以上游戏云服务器 立即下载
网站/服务器取证 实践与挑战 立即下载
ECS计算与存储分离架构实践 立即下载