我需要在我的Android应用程序中实现登录功能,我是Android编程中的新手
我有登录活动,这也是我的主要活动,所以我读到了AsyncTask,用于在主要活动中实现HTTP请求,我按照自己的方式进行操作。
我的问题是我的代码似乎是正确的但它不是:)并且在DDMS中抛出我和错误
这是我的登录活动代码:
package ir.alldigitall.pos;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import android.os.AsyncTask;
public class Login extends Activity
{
public String auth="";
protected void onCreate(Bundle paramBundle)
{
SharedPreferences userDetails = getSharedPreferences("userdetails", 0);
String user = userDetails.getString("username", "");
String pass = userDetails.getString("password", "");
super.onCreate(paramBundle);
if ((user == "") && (pass == ""))
{
setContentView(R.layout.activity_login);
return;
}
startActivity(new Intent("ir.alldigitall.pos.CreateInvoice"));
}
private class ReadJSON extends AsyncTask<String,Void,String>
{
protected String doInBackground(String...Detail)
{
Adapter adb=new Adapter();
String result=adb.login(Detail[0], Detail[1]);
return result;
}
protected void onPostExecute(String result)
{
auth=result;
}
}
public void onClickLoginBtn(View paramView)
{
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
SharedPreferences userDetails = getSharedPreferences("userdetails", 0);
CheckBox ch_remember = (CheckBox)findViewById(R.id.ch_remember);
EditText txt_StaffID = (EditText)findViewById(R.id.txt_StaffID);
EditText txt_password = (EditText)findViewById(R.id.txt_password);
String[] ar=new String[2];
ar[0]=txt_StaffID.getText().toString();
ar[1]=txt_password.getText().toString();
new ReadJSON().execute(ar);
if (ch_remember.isChecked())
{
SharedPreferences.Editor detailEditor = userDetails.edit();
detailEditor.clear();
detailEditor.putString("username", txt_StaffID.getText().toString().trim());
detailEditor.putString("password", txt_password.getText().toString().trim());
detailEditor.commit();
Toast.makeText(this, auth.toString(), Toast.LENGTH_LONG).show();
startActivity(new Intent("ir.alldigitall.pos.CreateInvoice"));
}
else
{
Toast.makeText(this, auth.toString(), Toast.LENGTH_LONG).show();
startActivity(new Intent("ir.alldigitall.pos.CreateInvoice"));
}
}
}
我的JSON Adapbter是:
package ir.alldigitall.pos;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class Adapter {
public String login(String uname,String password)
{
InputStream is = null;
String result = "";
JSONObject JObject=null;
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = newHttpPost("http://10.0.2.2/pos/pos.asmx?op=Authenticate");
nameValuePairs.add(new BasicNameValuePair("StaffID", uname));
nameValuePairs.add(new BasicNameValuePair("Password", password));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch (IOException e)
{
Log.e("log_tag", "Error in http connection " + e.getLocalizedMessage());
}
// convert response to string
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader( is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.v("log","Result :"+result);
//Convert String to JSON
try{
JObject = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.getLocalizedMessage());
}
}
catch (Exception e)
{
Log.v("log", "Error converting result " + e.getLocalizedMessage());
}
return JObject.toString();
}
}
最后我的错误日志: