发布身份验证和获取请求Restful Android

时间:2013-12-29 10:03:13

标签: android

我在网址http://api.olhovivo.sptrans.com.br/v0

中有这个RestFul
POST /Login/Autenticar?token={token} 

RETURN 真

GET /Linha/Buscar?termosBusca={termosBusca} 
RETURN
[
    {
        "CodigoLinha": 1273,
        "Circular": false,
        "Letreiro": "8000",
        "Sentido": 1,
        "Tipo": 10,
        "DenominacaoTPTS": "PCA.RAMOS DE AZEVEDO",
        "DenominacaoTSTP": "TERMINAL LAPA",
        "Informacoes": null
    },
    {
        "CodigoLinha": 34041,
        "Circular": false,
        "Letreiro": "8000",
        "Sentido": 2,
        "Tipo": 10,
        "DenominacaoTPTS": "PCA.RAMOS DE AZEVEDO",
        "DenominacaoTSTP": "TERMINAL LAPA",
        "Informacoes": null
    }
]

如何在android中的GET方法中验证并获取Json记录。

由于

3 个答案:

答案 0 :(得分:0)

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, this.CONNECT_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParameters, this.CONNECT_TIMEOUT);

DefaultHttpClient client = new DefaultHttpClient(httpParameters);

// Retrieve user credentials.
SharedPreferences settings = context.getSharedPreferences("LOGIN", 0);
String loginNameString = settings.getString("login_name", null);
String passwordString = settings.getString("password", null);

UsernamePasswordCredentials credentials = 
         new UsernamePasswordCredentials(loginNameString, passwordString);

AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT);
client.getCredentialsProvider().setCredentials(authScope,                                                                              credentials);

HttpPut put = new HttpPut(UPLOAD_URL);

try
{
    put.setEntity(new StringEntity(responseJSONArray.toString(),    
                   SERVER_PREFERRED_ENCODING));
    put.addHeader("Content-Type", "application/json");
    put.addHeader("Accept", "application/json");
    HttpResponse response = client.execute(put);

    // 200 type response.
    if (response.getStatusLine().getStatusCode() >= HttpStatus.SC_OK &&
    response.getStatusLine().getStatusCode() < HttpStatus.SC_MULTIPLE_CHOICES)
    {
      // Handle OK response etc........
    }
}
catch (Exception e)
{

}

答案 1 :(得分:0)

在Fiddler作曲家中我有这个命令非常有效: 发布http://api.olhovivo.sptrans.com.br/v0/Login/Autenticar?token=03d46461ccbf04b6ae98629e563cedcaf153bcdb31f9aa2c531a9a08e7aab7bb

但是当我尝试在Android中请求相同的命令时,我收到错误:

我的代码是:

  DefaultHttpClient client1 = new DefaultHttpClient();      
  HttpPost post = new HttpPost("http://api.olhovivo.sptrans.com.br/v0/Login/Autenticar?token=03d46461ccbf04b6ae98629e563cedcaf153bcdb31f9aa2c531a9a08e7aab7bb");
  post.setHeader("Content-Length", "0");      
  post.setHeader("Content-Type", "charset=utf-8");
  HttpResponse response1 = null;
  try {

      response1 = client1.execute(post);

    } catch (ClientProtocolException e) 
    {
        msg = "Servidor indisponível. Tente mais trade.";   
        return result;

    } 
  catch (IOException e) {
      msg = "Servidor indisponível. Tente mais trade."; 
        return result;
    }

答案 2 :(得分:0)

您必须从身份验证响应中捕获Cookie(截至今天,唯一返回的Cookie为apiCredentials),并将其包含在您的下一个请求中。

(它有一些超时,因此您需要在需要时通过再次进行身份验证来刷新它。)

相关问题