让我描述一下我的申请, 我正在从网站 JSON url(Drupal 网站)获取数据,数据为 JSON 格式。 在我的应用程序中,登录功能完美运行。 & 用户在服务器上得到验证。 我还从服务器获取其他数据(JSON url)并显示在我的 android 应用程序中。 现在,问题是我无法访问需要登录的页面的 JSON 数据,因为我的登录没有在整个 android 应用程序中维护。
我在 stackoverflow 和谷歌上搜索过,我得到了这些链接并尝试过,但不知道如何在我的代码中使用它们: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/statemgmt.html
这是未登录的 drupal 站点的空 JSON。
{
"nodes": []
}
这是来自 drupal 站点的 JSON - 登录后( http://www.mywebsite.com/user/login ) & 重新加载页面 http://www.mywebsite.com/myaccount-page在网站上 - 在计算机网络浏览器中。表示计算机网络浏览器自动维护登录 session 。
{
"nodes": [
{
"node": {
"Uid": "51",
"Username": "anand",
"Name": "anand",
"Address": "\n\tAt- vadodara Nr. Kareli Baugh",
"Date of Birth": "1998-08-20",
"Occupation": "student",
"Member Since": "36 weeks 6 days"
}
}
]
}
但在 android 应用程序中它不会自动执行此操作。 所以我想在 Android 中维护此 session ,以便我可以登录 android 应用程序,登录后重定向到另一个页面 Activity 并在那里获取 JSON 数据。 这是我的代码:
LoginActivity.java
public void onClick(View v) {
String uName = editUser.getText().toString();
String Password = editPass.getText().toString();
if(uName.equals("") | Password.equals(""))
{
Toast.makeText(getApplicationContext(), "Enter the Username and Password",Toast.LENGTH_SHORT).show();
}
else{
String strResponse = util.makeWebCall(loginURL,uName,Password);
System.out.println("=========> Response from login page=> " + strResponse);
try{
if (strResponse.substring(KEY_SUCCESS) != null) {
txterror.setText("");
Intent inlogin = new Intent(LoginActivity.this,
post_myprofile.class);
inlogin.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(inlogin);
//finish();
}
else
{
txterror.setText("Username and Password Not valid !!!");
}
}
catch (Exception e) {
// TODO: handle exception
}
}
}
});
btngotoregister.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent(getApplicationContext(),
RegisterActivity.class);
// intent.setFlags (Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent1);
}
});
}
}
util.java 中的 makeWebCall 方法
util.java
public static String makeWebCall(String url, String uname,String pass)
{
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username",uname));
params.add(new BasicNameValuePair("password",pass));
UrlEncodedFormEntity formEntity = null;
try {
formEntity = new UrlEncodedFormEntity(params);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
post.setEntity(formEntity);
try {
//post.setEntity(new StringEntity(requestString));
HttpResponse response = client.execute(post);
System.out.println("=========> Responsehello => "+response);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK)
{
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
return iStream_to_String(is);
}
else
{
return "Hello This is status ==> :"+String.valueOf(statusCode);
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
现在使用此代码登录成功并且我从服务器获得了包含详细信息的 JSON 响应。 & page-activity 重定向到用户配置文件的第二页。 在第 2 页上,我没有获得用户配置文件 JSON 数据 - 如上所述,我得到的是空白 JSON,因为未维护 session 。
这是第二个页面 Activity 的代码。
post_myprofile.java
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
String url = "http://www.cheerfoolz.com/myaccount-page";
String strResponse = util.makeWebCall(url);
try {
JSONObject objResponse = new JSONObject(strResponse);
JSONArray jsonnodes = objResponse
.getJSONArray(API.cheerfoolz_myprofile.NODES);
util.java 中配置文件的makewebcall 方法
工具.java
public static String makeWebCall(String url) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpRequest = new HttpGet(url);
// HttpPost post = new HttpPost(url);
try {
HttpResponse httpResponse = client.execute(httpRequest);
final int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
/* Log.i(getClass().getSimpleName(),
"Error => " + statusCode + " => for URL " + url);*/
return null;
}
HttpEntity entity = httpResponse.getEntity();
InputStream is = entity.getContent();
return iStream_to_String(is);
}
catch (IOException e) {
httpRequest.abort();
// Log.w(getClass().getSimpleName(), "Error for URL =>" + url, e);
}
return null;
}
public static String iStream_to_String(InputStream is1)
{
BufferedReader rd = new BufferedReader(new InputStreamReader(is1), 4096);
String line;
StringBuilder sb = new StringBuilder();
try {
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String contentOfMyInputStream = sb.toString();
return contentOfMyInputStream;
}
}
我在这个页面的这里得到了空白的 JSON - 我在上面已经提到过。那么如何在此用户个人资料 Activity 中保持 session 并获取数据?
感谢收听。
最佳答案
终于对我有用了:)
我没有一直使用新的 DefaultHttpClient,而是将其设为静态并且只使用一次。
static DefaultHttpClient client = new DefaultHttpClient();
关于android - 如何在整个android原生应用程序中维护服务器登录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10510846/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
我正在尝试使用ruby和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我
我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
我想用ruby编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除