我们有使用api(web服务)开发的php(codeIgniter)Web应用程序我试图使用相同的api开发Android应用程序。 我能够使用Web服务api调用登录但无法获取json数据并获得错误401(未经授权)
数据提取Web服务由oauth_timestamp和oauth_nonce以及Oauth_token组成,我无法在android中维护cookie。
我正在使用截击来访问网络服务。
请帮助我尽快解决这个问题......
public long TimeStamp(){
/*Long tsLong = System.currentTimeMillis()/1000;
String ts = tsLong.toString();
return ts;*/
long t = new Date().getTime();
return (long) Math.floor(t/1000)+3600*12;
}
String CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
int length=6;
public String nonce(int length){
String chars = CHARS;
String res="";
for (int i = 0; i < length; ++i) {
int rnum = (int) Math.floor(Math.random() * chars.length());
res = res + chars.substring(rnum, rnum + 1);
}
return res;
}
//获取json数据且无法维护会话ID 令牌=&#34; ksd934nksdj9sd9sdsdfiu3&#34; secret =&#34; sd2fgi94jfk&#34; ci_session =&#34; w2jksd23jckjdsdkksdl3kd92kje&#34; ;
String url4 = "http://localhost/app/services/CaptureMeasure/getFacilityListDetail/?oauth_token="+token+"&oauth_token_secret="+secret+"&oauth_consumer_key=www.sthub.com&oauth_signature_method=PLAINTEXT&oauth_version=1.0&oauth_timestamp="+TimeStamp()+"&oauth_nonce="+nonce(6)+"&oauth_signature=sthub123456789%26"+secret;
//Toast.makeText(MainActivity.this, ""+last_token+"las----->"+last_secret, Toast.LENGTH_SHORT).show();
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest stringRequest3 = new StringRequest(Request.Method.GET, url4, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG+"---->",response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG,error.toString());
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Cookie",ci_session);
return headers;
}
};
requestQueue.add(stringRequest3);