访问使用Android的HTTP GET方法登录API [英] Accessing a login api using http get method in android

查看:112
本文介绍了访问使用Android的HTTP GET方法登录API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Android应用程序,它使用一个登录的API,这将允许其互联网用户与他们的Andr​​oid设备......

在相同的凭据登录

对于API的URL是

<$p$p><$c$c>https://api.ecoachsolutions.com/main.php?ecoachsignin=1&server=remote&user=ecoachguest&pass=ecoachguest

这retuns在JSON的响应

  JSON对象:{
    状态:其中,成功或错误&gt;中
    MSG:或其可响应消息&gt;中
    简介:&lt;使用者轮廓对象&gt;
}

我想这code,我发现在互联网上搜索,但它不工作,

 私人无效doLogin(查看视图){
    //警报消息
          _spinner.setVisibility(View.VISIBLE);
    Toast.makeText(mContext,连接到服务器......,
            Toast.LENGTH_SHORT).show();
    // URLEn code用户定义的数据
    。字符串usernameValue = username.getText()的toString();    。字符串passValue = password.getText()的toString();
    //创建HTTP cliient对象发送请求给服务器    HttpClient的客户端=新DefaultHttpClient();    //创建URL字符串    字符串的URL = \"https://api.ecoachsolutions.com/main.php?ecoachsignin=1&server=remote&user=\"+usernameValue+\"&pass=\"+passValue;    Log.i(HTTPGET,URL);    尝试
    {
        串SetServerString;        //创建请求的服务器,并得到响应
        HTTPGET HTTPGET =新HTTPGET(URL);        ResponseHandler所&LT;串GT; ResponseHandler所=新BasicResponseHandler();        SetServerString = Client.execute(HTTPGET,ResponseHandler所);        的System.out.println(usernameValue);
        的System.out.println(passValue);
        //显示上的活动响应        Toast.makeText(getBaseContext(),SetServerString,Toast.LENGTH_LONG).show();
    }
    赶上(异常前)
    {
        Toast.makeText(getBaseContext(),失败,Toast.LENGTH_LONG).show();
        _spinner.setVisibility(View.INVISIBLE);
    }
}

将AP preciate的帮助或正向感谢:)


解决方案

更改code,以获得的Htt presponse 像下面,

 字符串responseBody =;        HTT presponse响应= client.execute(岗位);        。INT响应code = response.getStatusLine()的getStatus code();        Log.i(GET响应code,响应code +);        开关(响应code){
        //意味着服务器响应
        案例200:
        HttpEntity实体= response.getEntity();
            如果(实体!= NULL){
                responseBody = EntityUtils.toString(实体);
                //现在你可以试着在这里打印您返回的字符串,你去为JSON解析之前
            }
            打破;
        //添加更多的case语句来处理其他方案
        }

在code是简单,但如果还是无法理解,不要犹豫,问。

I am developing an android app which uses a login api, which will allow its web users to login with their same credentials on the android device.....

the url for the api is

https://api.ecoachsolutions.com/main.php?ecoachsignin=1&server=remote&user=ecoachguest&pass=ecoachguest

which retuns a response in json

JSON object: {
    status: <success or error>,
    msg: <response message>,
    profile: <user profile object> 
}

I tried this code which I found searching on the internet but it isn't working,

private void doLogin(View view) {
    //ALERT MESSAGE
          _spinner.setVisibility(View.VISIBLE);
    Toast.makeText(mContext, "connecting to server.... ",
            Toast.LENGTH_SHORT).show();


    // URLEncode user defined data


    String usernameValue   = username.getText().toString();

    String passValue    = password.getText().toString();


    // Create http cliient object to send request to server

    HttpClient Client = new DefaultHttpClient();

    // Create URL string

    String URL = "https://api.ecoachsolutions.com/main.php?ecoachsignin=1&server=remote&user="+usernameValue+"&pass="+passValue;

    Log.i("httpget", URL);

    try
    {
        String SetServerString ;

        // Create Request to server and get response
        HttpGet httpget = new HttpGet(URL);

        ResponseHandler<String> responseHandler = new BasicResponseHandler();

        SetServerString = Client.execute(httpget, responseHandler);

        System.out.println(usernameValue);
        System.out.println(passValue);
        // Show response on activity

        Toast.makeText(getBaseContext(),SetServerString,Toast.LENGTH_LONG).show();
    }
    catch(Exception ex)
    {
        Toast.makeText(getBaseContext(),"Fail",Toast.LENGTH_LONG).show();
        _spinner.setVisibility(View.INVISIBLE);
    }


}

will appreciate the help or the positive direction thanks :)

解决方案

Change your code to get the HttpResponse like below,

        String responseBody = "";

        HttpResponse response = client.execute(post);

        int responseCode = response.getStatusLine().getStatusCode();

        Log.i("GET Response Code ",responseCode + "");

        switch(responseCode) {
        // Means server is responding
        case 200:    
        HttpEntity entity = response.getEntity();
            if(entity != null) {
                responseBody = EntityUtils.toString(entity);
                // Now you can try printing your returned string here, before you go for JSON parsing
            }
            break;
        // Add more case statements to handle other scenarios
        }

The code is simple, but if still unable to understand, don't hesitate to ask.

这篇关于访问使用Android的HTTP GET方法登录API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆